Seem that OrientDB doesn't return the correct result for the simple search.
I created a class node item with 2 properties id and flag
id: type STRING, index: UNIQUE
flag: type DECIMAL
flag can be set by 1, 0, or null value.
I use the query below to get all item which has flag is 1
`select from item where flag = 1`
But the query returns nothing.
Note: I have tested on 2.2.7 and 2.2.10, and seem that this issue only occurs if flag was defined in schema before feeding data.
Is it bug?
UPDATED: Added sample database. Get it here
If you use select from item where 1 = flag it works.
For your query select from item where flag = 1 could you open an issue on github attaching your database ?
Tested on 2.2.10, no problems here:
create class item extends v
create property item.id STRING
create property item.flag DECIMAL
insert into item(id,flag) values ("id1",1)
select from item where flag = 1
Also added UNIQUE index on id from studio -> schema.
Output:
Related
I am using jsonb type in a column in postgresql11. And I'd like to update one field in the json data and I see there is a function jsonb_set which can be used. (https://www.postgresql.org/docs/current/functions-json.html).
However, based on the document,
jsonb_set ( target jsonb, path text[], new_value jsonb [, create_if_missing boolean ] ) → jsonb
Returns target with the item designated by path replaced by new_value, or with new_value added
if create_if_missing is true (which is the default) and the item designated by path does not
exist. All earlier steps in the path must exist, or the target is returned unchanged. As with
the path oriented operators, negative integers that appear in the path count from the end of
JSON arrays. If the last path step is an array index that is out of range, and create_if_missing
is true, the new value is added at the beginning of the array if the index is negative, or at
the end of the array if it is positive.
The first argument is target. What does target mean here? Do I need to do a query to get existing value and put it as target?
I have tried below update statement:
my current data is:
# select "taxes" from "Sites" where "id" = '6daa9b5d-d5b2-4b0d-a8ee-5ad2cb141594';
taxes
--------------------------------------------------------------------------------------------------------------
{"feePercent": 0, "percent": 0}
And I tried below update:
# update "Sites" set "feePercent" = jsonb_set('{"feePercent": 0, "percent": 0}', '{feePercent}', 1) where "siteUuid"='6daa9b5d-d5b2-4b0d-a8ee-5ad2cb141594';
but I got below error:
ERROR: function jsonb_set(unknown, unknown, integer) does not exist
LINE 1: update "Sites" set "feePercent" = jsonb_set('{"feePerce...
jsonb_set() modifies a specific JSON object. So, your target is the JSON object (or JSON column) which you want to modify.
jsonb_set(my_jsonb_to_be_modified, ...)
So, if you had this JSON object;
{"my":"old", "json":"object"}
With the function you can turn it into:
{"my":"new", "json":"object"}
The code is:
demo:db<>fiddle
SELECT jsonb_set('{"my":"old", "json":"object"}', '{my}', '"new"')
The target is the original JSON object, the path points to the element you want to modify, and new_value is the new value for the element you specified in the path.
In that case my had the value old, which turns into new now.
From PostgreSQL v14 on, you can use subscripts to make this UPDATE statement look natural:
UPDATE "Sites"
SET taxes['feePercent'] = to_jsonb(1)
WHERE id = '6daa9b5d-d5b2-4b0d-a8ee-5ad2cb141594';
For earlier versions, you will have to use jsonb_set like this:
UPDATE "Sites"
SET taxes = jsonb_set(taxes, ARRAY['feePercent'], to_jsonb(1))
WHERE id = '6daa9b5d-d5b2-4b0d-a8ee-5ad2cb141594';
The effect is the same: the whole JSON is read, a new JSON is created and stored in a new version of the row.
All this becomes much simpler if you don't use JSON, but regular table columns.
I am trying to update a JSONB field in one table with data from another table. For example,
update ms
set data = data || '{"COMMERCIAL": 3.4, "PCT" : medi_percent}'
from mix
where mix.id = mss.data_id
and data_id = 6000
and set_id = 20
This is giving me the following error -
Invalid input syntax for type json
DETAIL: Token "medi_percent" is invalid.
When I change medi_percent to a number, I don't get this error.
{"COMMERCIAL": 3.4, "PCT" : medi_percent} is not a valid JSON text. Notice there is no string interpolation happening here. You might be looking for
json_build_object('COMMERCIAL', 3.4, 'PCT', medi_percent)
instead where medi_percent is now an expression (that will presumably refer to your mix column).
In a Postgres DB I have a field field defined like this:
CREATE TABLE t (
id SERIAL PRIMARY KEY,
field character varying(255)[] DEFAULT ARRAY[]::character varying[],
);
There I store values like:
ID FIELD
1 {{lower,0},{greater,10}}
2 {{something_else,7},{lower,5}}
1 - How can I select the lower/greater value? I'd like a query response like this:
ID LOWER
1 0
2 5
2 - How can I filter by those lower/greater values?
Thanks!
It's pretty awkward to do but this accomplishes it. I use PG 9.3 so I don't know if there are better ways to do this in later versions.
SELECT id, (SELECT field[ss][2] FROM generate_subscripts(field, 1) ss WHERE field[ss][1] = 'lower') AS lower
FROM t;
Basically, for each record, generate the subscripts to use as indexes into the main array to access the subarrays. For each, look for an array where the first item is 'lower'. If found, return the value of the second item.
I am facing issues in retrieving the Latest value from the MongoDB table (Collection).
My table doesn't have a class mapped. I cannot create a class for my table due to the dynamic behaviour.
For Eg
MyTable has 4 columns mentioned below is the table and values.
Id , DateTime, Column1, Column2
1 somTime 1 1
2 somTime 2 2
3 somTime 3
Due to the user requirements the table is dynamic, the user can create as many columns he want and he can insert values in to these columns separately.
In this I want to get the latest value inserted in the Column2 which is 2.
My code looks like this.
var maxDateTime = (from collect in document.AsQueryable()
select collect["dateTime"]).Max();
var qLatestValue = Query.EQ("DateTime", maxDateTime);
value = result["Column1"];
// this works fine since the Column1 exists
value = result["Column2"];
// this code is giving exception
The Column2 is not present in the document retrieved. Due to this its throwing keynotFound exception.
How to get the latest value of this Column2?
Can any one please help in this?
Your result doesn't have a filed named Column2 in your document. The solution is getting value via
Way 1:
result["Column2", BsonNull.Value]
Way 2:
result.GetValue("Column2", BsonNull.Value);
Way 3:
BsonValue value;
var hasValue = doc.TryGetValue("Column2");
When you are retrieving the maxDateTimevalue you are retrieving the most recent one in the collection but this document may not contain the Column2 key
You will need to include a Where clause in your maxDateTime query any only get the maxDateTime for documents where the Column2 key exists.
e.g
Query.Exists("Column2")
Sorry for the late reply. Below code fixed my issue.
var maxDateTime =(from a in document.AsQueryable() where c[columnName] != BsonNull.Value select c["DateTime"]).Max();
var qlatest= Query.EQ("DateTime", maxDateTime);
var result = document.FindOne(qlatest);
value = result[columnName];
How to set an object to null.
Ex:
my object samp contains three fileds
samp.field1,samp.field2.sampfield3
If i set samp:= null;
im getting errors is there a way to set the object value to null.
An sql database does not know about objects, it deals with rows in table.
To remove a row use DELETE :
e.g. :
DELETE FROM samp WHERE id = 12345;
DELETE FROM samp WHERE field1 = 'Delete Me';
The first example is typical to remove individual rows uing their primary key (id in this case)
The second example will remove a group of rows which have a speciic value for a field.