Suklesh Rao
Suklesh Rao

Reputation: 45

How to write a Cosmos DB query to get child array without the child array name

Is it possible to write a query to get the child array without it's name?

I know two methods to get a child array, but both of the, return the JSON file with the child array name included. Is there any way to not get that child array name

Expected output:

    [
        {
            "name": "acvb",
            "addr": "bca",
            "age": 20
        },
        {
            "name": "zxc",
            "addr": "evd",
            "age": 22
        }

]
       

Actual output:

[
    {
        "event": [
            {
                "name": "acvb",
                "addr": "bca",
                "age": 20
            },
            {
                "name": "zxc",
                "addr": "evd",
                "age": 22
            } 
         ]  
      },
   {}
 ]

Query that I am using

SELECT c.event FROM c

Also I noticed that I am getting back an empty object {} after the array of objects. I running it on Cosmos DB and got this result, it seems I get this no matter how I give the query. Is there any way to fix this?

Upvotes: 2

Views: 566

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136379

Assuming your input data looks like:

{
    "id": "something",
    "_partitionKey": "partition key value",
    "event": [
        {
            "name": "acvb",
            "addr": "bca",
            "age": 20
        },
        {
            "name": "zxc",
            "addr": "evd",
            "age": 22
        }
    ]
}

You can use the following query:

SELECT *
FROM c IN t.event

It will produce the following output:

[
    {
        "name": "acvb",
        "addr": "bca",
        "age": 20
    },
    {
        "name": "zxc",
        "addr": "evd",
        "age": 22
    }
]

For more details, please see this blog post: https://devblogs.microsoft.com/cosmosdb/understanding-how-to-query-arrays-in-azure-cosmos-db/.

Upvotes: 2

Related Questions