首页 > 解决方案 > Cosmos db 查询以获取数组项不为空的数据

问题描述

我的 cosmos db 容器中有 Collection 项目。我的示例数据是

{
    "objectID": "abc",
    "id": "123",
    "name": "gfh",
    "description": "chock",
    "brand": "hcn",
    "size": 180,
    "sizeUnits": "Grams",
    "stores": []
},
{
    "objectID": "123c",
    "id": "0dfg",
    "name": "shaek",
    "description": "7ihk",
    "brand": "fghcn",
    "size": 768,
    "sizeUnits": "Grams",
    "stores": [ {
            "id": 678",
            "price": 2.2
        },
{
 "id": 678",
            "price": 2.2}]
}

等等...

我需要获取存储为空的所有详细信息。如何为此编写查询。

标签: azure-cosmosdbazure-cosmosdb-sqlapi

解决方案


请尝试以下 SQL:

SELECT * FROM c where ARRAY_LENGTH(c.stores) > 0

结果:

[
    {
        "objectID": "123c",
        "id": "0dfg",
        "name": "shaek",
        "description": "7ihk",
        "brand": "fghcn",
        "size": 768,
        "sizeUnits": "Grams",
        "stores": [
            {
                "id": 678,
                "price": 2.2
            },
            {
                "id": 678,
                "price": 2.2
            }
        ]
    }
]

推荐阅读