Imagine we have a such collection (example taken from https://www.documentdb.com/sql/demo)
{
    "_id" : "19015",
    "description" : "Snacks, granola bars, hard, plain",
    "servings" : [ 
        {
            "amount" : 1,
            "description" : "bar",
            "weightInGrams" : 21
        }, 
        {
            "amount" : 1,
            "description" : "bar (1 oz)",
            "weightInGrams" : 28
        }, 
        {
            "amount" : 1,
            "description" : "bar",
            "weightInGrams" : 25
        }
    ]
}
How i can query CosmosDB in SQL api to get results such like this?
{
    "_id" : "19015",
    "servings" : [ 
        {
            "description" : "bar"
        }, 
        {
            "description" : "bar (1 oz)"
        }, 
        {
            "description" : "bar"
        }
    ]
}
at MongoDB i would use such query
db.getCollection('food').find({id: '19015'}, {'servings.description' : 1})
Tried multiple scenarious with joins etc
SELECT 
    food.id,
    food.servings.description
FROM food
WHERE food.id = "19015"
or
SELECT 
    food.id,
    [{
        description: food.servings[0].description
    }] AS servings
FROM food
WHERE food.id = "19015"
[0] i tried [] or [$] but doesn't worksDoes somebody has idea how in simply way i can resolve this issue?
You could use the ARRAY(subquery) expression to achieve this. Here's the query:
SELECT
    food.id,
    ARRAY(SELECT serving.description FROM serving IN food.servings) AS servings
FROM food
WHERE food.id = "19015"
                        By executing this query:
select food.id, servings.description
from food join servings in food.servings
you would at least get the following result:
{"id":19015, "description":"bar"}
{"id":19015, "description":"bar (1 oz)"}
{"id":19015, "description":"bar"}
Not 100% what you were expecting, but maybe something you can work with!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With