3

I have a query for CosmosDB:

SELECT food.tags FROM food

which returns this:

{
  "tags": [
    {
      "name": "babyfood"
    },
    {
      "name": "dessert"
    },
    {
      "name": "fruit pudding"
    },
    {
      "name": "orange"
    },
    {
      "name": "strained"
    }
  ]
}

I would like to create a query to obtain results like:

["babyFood", "dessert", "fruit pudding", "orange", "strained"] 

so my goal is to get an array which will contain values of 'name' field from all objects in 'tags' array.

How could it be done in CosmosDB SQL?

Please help.

1 Answer 1

6

SELECT VALUE t.name from f join t in food.tags

with VALUE you do the flattening.

Sign up to request clarification or add additional context in comments.

5 Comments

This would return values as separated values not in an array which I want. My solution was to create a User Defined Function in JavaScript and use it in a query.
actually this will return an array of values. i
previous edit was not quite done..Actually this will return exactly the array you mentioned above. What kind of result did you get? Or was this just because of the typo in SELECT VALUE t.name from f join t in food.tags, which should be SELECT VALUE t.name from f join t in f.tags
How to get object fields name?
which object's name do you want to get?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.