过滤嵌套数组项 CosmosDb

时间:2020-12-18 21:30:41

标签: nosql azure-cosmosdb azure-cosmosdb-sqlapi

是否可以在 CosmosDb 中过滤数组项?例如我只需要客户信息和第一个宠物(在数组中)

当前结果:

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

预期:

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

1 个答案:

答案 0 :(得分:0)

您可以使用 ARRAY_SLICE 函数。

SQL:

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

结果:

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