0
votes

Is it possible to filter array items in CosmosDb? for example I just need customer info and the first pet(in an array)

Current result:

[
{
    "CustomerId": "100",
    "name": "John",
    "lastName": "Doe",
    "pets": [
        {
            "id": "pet01",
            "CustomerId": "100",
            "name": "1st pet"
        },
        {
            "id": "pet02",
            "CustomerId": "100",
            "name": "2nd pet"
        }
    ]
}
]

Expected:

[
{
    "CustomerId": "100",
    "name": "John",
    "lastName": "Doe",
    "pets": [
        {
            "id": "pet01",
            "CustomerId": "100",
            "name": "1st pet"
        }
    ]
}
]
1

1 Answers

0
votes

You can use ARRAY_SLICE function.

SQL:

SELECT c.CustomerId,c.name,c.lastName,ARRAY_SLICE(c.pets,0,1) as pets 
FROM c

Result:

[
    {
        "CustomerId": "100",
        "name": "John",
        "lastName": "Doe",
        "pets": [
            {
                "id": "pet01",
                "CustomerId": "100",
                "name": "1st pet"
            }
        ]
    }
]