I have a document with the following JSON.
{
"id": "123",
"user": {
"456": true
}
}
When I write SQL the key '456' is illegal.
SELECT * FROM c where c.user.456 = true
Is there a way to user numbers as key in the query above? Also if I have a key with a period in it, the SQL search doesn't work.
Please try something like this:
SELECT * FROM c where c.user['456'] = true
Hope this can help you:).
Related
Is it possible to create some sort of passthrough response resolver for an RDS datasource. The result of the query is 1 row and 1 column of type json which I would like to be the result of the graphql query.
Eg. schema could be
type Query {
getJsonFromDb(p: String): Res
}
type Res {
prop: String
}
schema {
query: Query
}
The request resolver could be
{
"version": "2018-05-29",
"statements": [
"select json_build_object('prop',:P)"
],
"variableMap": {
":P": $util.toJson($ctx.arguments.p)
}
}
In the log result logging looks like this
"result": "{\"sqlStatementResults\":[{\"columnMetadata\":[{\"arrayBaseColumnType\":0,\"isAutoIncrement\":false,\"isCaseSensitive\":true,\"isCurrency\":false,\"isSigned\":false,\"label\":\"json_build_object\",\"name\":\"json_build_object\",\"nullable\":0,\"precision\":2147483647,\"scale\":0,\"schemaName\":\"\",\"tableName\":\"\",\"type\":1111,\"typeName\":\"json\",\"signed\":false,\"autoIncrement\":false,\"caseSensitive\":true,\"currency\":false}],\"numberOfRecordsUpdated\":0,\"records\":[[{\"stringValue\":\"{\\\"prop\\\" : \\\"test\\\"}\"}]]}]}"
So it's there allright, but I cannot seem to find the correct velocity expressions to get it out.
All tips certainly appreciated!
Peter
So I found one way of getting this done by doing like in the response mapping
$util.parseJson($ctx.result).sqlStatementResults[0].records[0][0].stringValue
How can CosmosDB Query the values of the properties within a dynamic JSON?
The app allows storing a JSON as a set of custom properties for an object. They are serialized and stored in CosmosDb. For example, here are two entries:
{
"id": "ade9f2d6-fff6-4993-8473-a2af40f071f4",
...
"Properties": {
"fn": "Ernest",
"ln": "Hemingway",
"a_book": "The Old Man and the Sea"
},
...
}
and
{
"id": "23cb9d4c-da56-40ec-9fbe-7f5178a92a4f",
...
"Properties": {
"First Name": "Salvador",
"Last Name": "Dali",
"Period": "Surrealism"
},
...
}
How can the query be structured so that it searches in the values of Properties?
I’m looking for something that doesn’t involve the name of the
sub-propety, like SELECT * FROM c WHERE
some_function_here(c.Properties, ‘Ernest’)
Maybe I get your idea that you want to filter the documents by the value of the Properties, not the name. If so , you could use UDF in cosmos db.
sample udf:
function query(Properties,filedValue){
for(var k in Properties){
if(Properties[k] == filedValue)
return true;
}
return false;
}
sample query:
SELECT c.id FROM c where udf.query(c.Properties,'Ernest')
output:
Just summary here, Ovi's udf function like:
function QueryProperties (Properties, filedValue) {
for (var k in Properties) {
if (Properties[k] && Properties[k].toString().toUpperCase().includes(filedValue.toString().toUpperCase()))
return true;
return false;
}
Both of the following syntax's will work.
SELECT * FROM c where c.Properties["First Name"] = 'Salvador'
SELECT * FROM c where c.Properties.fn = 'Ernest'
I need to do a query in an Cloudant DataBase where you compare a number with decimals that is defined as a string with another number sent from the server. The problem is that a comparison of strings is made and I need it to be a numerical comparison. There is there any way to perform this search by converting the database parameter to float while doing the query? O there are another way to do this query?
This is the query in the server, value.precio is sent from the client as a string.
value.precio = value.precio.split("-");
var precio_init = value.precio[0];
var precio_final = value.precio[1];
value.precio = {
"$gte":precio_init,
"$lte":precio_final
};
And in my database this is the parameter I want to search is:
"precio": "13.39"
Thanks
I don't think you will be able to do this with Cloudant Query, but you could try Cloudant Search. Create a new search index similar to the following:
Design Doc: myDesignDoc
Index Name: byPrecio
Index:
function (doc) {
if (doc.precio) {
index("precio", parseFloat(doc.precio));
}
}
Then you can uses ranges to search. For example:
precio:[13 TO 14]
Full search on Cloudant would look like this:
https://xxx.cloudant.com/YOUR_DB/_design/myDesignDoc/_search/byPrecio?q=precio:[13%20TO%2014]&include_docs=true
Sample response:
{
"total_rows":1,
"bookmark":"g2wAAAAxxx",
"rows":[
{
"id":"74fa6ff1b6dbca8c10d677832f6a3de2",
"order":[
1.0,
0
],
"fields":{
},
"doc":{
"_id":"74fa6ff1b6dbca8c10d677832f6a3de2",
"_rev":"2-17c984e51102b719fe9f80fc5d5bc78e",
"precio":"13.39",
"otherField":"otherValue"
}
}
]
}
More info on Cloudant Search here
I am a complete novice in python. I have connected to a database in my cloudant account through python. I need to construct a query for filtering the data.
Here x is an unixdate object which i have calculated using the formula :-
s = str(datetime.date.today() - datetime.timedelta(days=2))
x = datetime.datetime.strptime(s, "%Y-%m-%d").timestamp()
Method 1:
query = Query(my_database, selector={"type": "add", "uid": { "$gt": 0 }, "$_viewts": {"$gt": x }})
This query raises an HTTP error stating
invalid operator for: $_viewts
Method 2:
query = Query(my_database, selector={"type": "add", "uid": { "$gt": 0 }, "_viewts": {"$gt": x }})
This query runs when I remove the $ sign from the key $_viewts but produces an empty list as a result.
Any solutions/suggestions would be of great help.
Thanks
Hi I have a test doc like this:
{
"doctype": "test",
"users": [
1,
2
]
}
Then I used below query to get the result:
SELECT * FROM bucket WHERE doctype = "test" AND ANY user IN users SATISFIES user = 1 END;
But I got this error: "msg": "syntax error - at user".
Who knows where I got wrong? Thanks
USER is a reserved word.
You can use u instead, or place USER in back ticks to escape it.