如何使用 cosmos db 中的 ArrayContains 方法检查字符串数组中的值?

How to check for a value that is inside a String array using ArrayContains method in cosmos db?

我有以下 JSON 并且我想在名称="style" 并且属性包含 US - Rectangle

时获取此记录
{
    "id": "5ede1c0f1b4b335ed3bf3bca",
    "attributes": [
        {
            "name": "Style",
            "properties": [
                "US - Rectangle",
                "US - Square"
            ]
        },
        {
            "name": "Button Configuration",
            "properties": [
                "2 button"
            ]
        },
        {
            "name": "Columns",
            "properties": [
                "Single Column"
            ]
        },
        {
            "name": "Colors",
            "properties": [
                "Beige"
            ]
        }
    ],

}

我已经写了这个查询

SELECT *
FROM c
where ARRAY_CONTAINS(c.attributes, {name:"Style"},true ) 

但无法理解如何包含属性值

您需要使用点访问器,如下所示:

select * from c where attributes[0].name="Style" and
attributes[0].properties="US - Rectangle"

这是自连接发挥作用的地方:

自连接属性数组,然后通过对属性执行 ARRAY_CONTAINS() 过滤。这是一个发出文档 ID 的示例,以及包含 属性 "US - Rectangle":

的特定属性名称
SELECT c.id,a.name FROM c
JOIN a IN c.attributes
WHERE ARRAY_CONTAINS(a.properties,"US - Rectangle")

输出:

[
    {
        "id": "5ede1c0f1b4b335ed3bf3bca",
        "name": "Style"
    }
]

如果您想将搜索限制为仅查看一个特定属性,只需将其添加到您的 WHERE 子句中:

SELECT c.id, a.name FROM c
JOIN a IN c.attributes
WHERE a.name = "Style"
AND ARRAY_CONTAINS(a.properties,"US - Rectangle")