duyn9uyen
duyn9uyen

Reputation: 10321

Query Nested Array in Cosmos DB

I'm new to Cosmos and I am trying to query a nested array. I found this blog on how to work with arrays but there isn't an example on a nested array.

Given this json document, how can I query the Tags array? I want to find all records that have a tag of "test-1" within its Fees array.

{
    "id": "22142846",
    "PartitionKey": 846,
    "Fees": [
        {
            "Tags": [
                "test-1"
            ]
        },
        {
            "Tags": [
                "test-2"
            ]
        }
    ]
}

Upvotes: 2

Views: 585

Answers (1)

RitikaNalwaya
RitikaNalwaya

Reputation: 590

Here is the sample query

SELECT c.id, c.PartitionKey
FROM c
JOIN t IN c.Fees
where array_contains(t.Tags,"test-1")

Here is the result.

enter image description here

Sample Records/Documents used

{
    "id": "22142846",
    "PartitionKey": 846,
    "Fees": [
        {"Tags": ["test-1"]},
        {"Tags": ["test-2"]}
    ]
}


{
    "id": "22142847",
    "PartitionKey": 847,
    "Fees": [
        {"Tags": ["test-1"]},
        {"Tags": ["test-2"]}
    ]
}

{
    "id": "22142848",
    "PartitionKey": 848,
    "Fees": [
       {"Tags": ["test-2"]},
       {"Tags": ["test-3"]}
    ]
}

Upvotes: 2

Related Questions