Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query CosmosDB for nested object value

How can I retrieve objects which match order_id = 9234029m, given this document in CosmosDB:

{
    "order": {
        "order_id": "9234029m",
        "order_name": "name",
    }
}

I have tried to query in CosmosDB Data Explorer, but it's not possible to simply query the nested order_id object like this:

SELECT * FROM c WHERE c.order.order_id = "9234029m"

(Err: "Syntax error, incorrect syntax near 'order'")

This seems like it should be so simple, yet it's not! (In CosmosDB Data Explorer, all queries need to start with SELECT * FROM c, but REST SQL is an alternative as well.)

like image 913
knutole Avatar asked May 23 '26 12:05

knutole


2 Answers

As you discovered, order is a reserved keyword, which was tripping up the query parsing. However, you can get past that, and still query your data, with slightly different syntax (bracket notation):

SELECT *
FROM c
WHERE c["order"].order_id = "9234029m"
like image 67
David Makogon Avatar answered May 26 '26 20:05

David Makogon


This was due, apparently, to order being a reserved keyword in CosmosDB SQL, even if used as above.

like image 43
knutole Avatar answered May 26 '26 19:05

knutole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!