根据 CosmosDb SQL 中子对象的属性过滤文档

问题描述 投票:0回答:1

我想获取 ui 模板中带有测试 url 的任何文档

例如: 输入

    {
        "id": "id1",,
        "EntityType": "metadata",
        "UITemplates": {
            "p1": [
                {
                    "DisplayText": "UiDefinition.json",
                    "UrlString": "https://1-test",
                    "SubType": "Custom"
                },
                {
                    "DisplayText": "createuidefinition",
                    "UrlString": "https://2-prod",
                    "SubType": "Custom"
                }
            ],
            "d3": [
                {
                    "DisplayText": "UiDefinition.json",
                    "UrlString": "https://11-test",
                    "SubType": "Custom"
                },
                {
                    "DisplayText": "createuidefinition",
                    "UrlString": "https://22-test",
                    "SubType": "Custom"
                }
            ]
        }
    }

输出: 文档在 UrlString 中具有“test”的 id 列表

[{"id", "id1"}]

阻碍我的是如何将listinguitemplates中的属性获取到数组中,我可以使用ArrayContains或稍后使用自连接将其过滤掉。

自加入但需要事先知道listinguitemplate下的属性名称

Tried to flatten but return empty
SELECT c
FROM c
JOIN p IN (
    SELECT VALUE item
    FROM item IN (
        SELECT VALUE t
        FROM t IN c.UITemplates
    )
)
If can know the fixed field of properties before
SELECT c.id, t.UrlString
FROM c
JOIN t IN c.UITemplates.p1

Result
[
    {
        "id": "id1",
        "UrlString": "https://1-test"
    },
    {
        "id": "id1",
        "UrlString": "https://2-prod"
    }
]

https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/join#self-join-with-a-single-item

https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/array-contains

有人可以为我照亮一些吗,谢谢

sql mysql azure azure-cosmosdb
1个回答
0
投票

发现有一个sql函数可以将对象属性映射到数组中 由于 in 运算符只作用于数组,而不作用于对象属性,所以需要先将属性转换为数组

SELECT DISTINCT c.id
FROM c
JOIN t in (SELECT VALUE ObjectToArray(c.UITemplates))
JOIN p in t.v
WHERE CONTAINS(p.UrlString, "test")
© www.soinside.com 2019 - 2024. All rights reserved.