在 Azure Cosmos DB 中通过 SQL 搜索嵌套的 JSON 对象

Searching nested JSON object's via SQL within Azure Cosmos DB

我在 Azure 托管的 Cosmos DB 容器中的项目中有这个 JSON 文件。

{
    "id": "1",
    "category": "apps",
    "apps": [
        {
            "id": "1",
            "category": "apps",
            "name": "testApp",
            "description": "I'm a test app!",
            "developer": "test developer",
            "created_at": "6/9/2021",
            "updated_at": "6/9/2021"
        },
        {
            "id": "2",
            "category": "apps",
            "name": "testAPp2",
            "description": "I'm a test app 2!",
            "developer": "test developer2",
            "created_at": "6/9/2021",
            "updated_at": "6/9/2021"
        }
    ]
}

我一直在尝试访问其中一个名称与我的参数匹配的应用程序:

SELECT *
FROM Items c
WHERE c.category = 'apps'
AND WHERE c.apps.name = 'testApp'

我可以通过查询访问应用程序

SELECT c.apps
FROM Items c
WHERE c.category = 'apps'

但我无法弄清楚如何访问应用程序数组中托管的单个应用程序之一。

I have been trying to access one of the apps whose name matches my parameter

你可以试试这个SQL:

SELECT value a
FROM Items c
JOIN a in c.apps
WHERE c.category = 'apps' and a.name = 'testApp'

结果:

[
    {
        "id": "1",
        "category": "apps",
        "name": "testApp",
        "description": "I'm a test app!",
        "developer": "test developer",
        "created_at": "6/9/2021",
        "updated_at": "6/9/2021"
    }
]

顺便说一句,如果你想访问应用程序数组中的特定项目,你可以使用 c.apps[index] 来获取它。比如

SELECT value c.apps[1] 
FROM c
where c.category = 'apps'

结果:

[
    {
        "id": "2",
        "category": "apps",
        "name": "testAPp2",
        "description": "I'm a test app 2!",
        "developer": "test developer2",
        "created_at": "6/9/2021",
        "updated_at": "6/9/2021"
    }
]