Home > Blockchain >  Query Nested Array in Cosmos DB
Query Nested Array in Cosmos DB

Time:01-18

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"
            ]
        }
    ]
}

CodePudding user response:

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"]}
    ]
}
  • Related