ARRAY_CONTAINS (Azure Cosmos DB)ARRAY_CONTAINS (Azure Cosmos DB)
适用于:
SQL API
返回一个布尔,它指示数组是否包含指定的值。Returns a Boolean indicating whether the array contains the specified value. 可以通过在命令中使用布尔表达式来检查对象的部分匹配或完全匹配。You can check for a partial or full match of an object by using a boolean expression within the command.
语法Syntax
ARRAY_CONTAINS (<arr_expr>, <expr> [, bool_expr])
参数Arguments
arr_exprarr_expr
是要搜索的数组表达式。Is the array expression to be searched.
exprexpr
是要找到的表达式。Is the expression to be found.
bool_exprbool_expr
是布尔表达式。Is a boolean expression. 如果它的计算值为“true”,并且指定的搜索值是对象,则该命令将检查部分匹配(搜索对象是其中一个对象的子集)。If it evaluates to 'true' and if the specified search value is an object, the command checks for a partial match (the search object is a subset of one of the objects). 如果它的计算值为“false”,则该命令将检查数组中所有对象的完全匹配。If it evaluates to 'false', the command checks for a full match of all objects within the array. 如果未指定,默认值为“false”。The default value if not specified is false.
返回类型Return types
返回一个布尔值。Returns a Boolean value.
示例Examples
以下示例演示了如何使用 ARRAY_CONTAINS
检查数组中的成员身份。The following example how to check for membership in an array using ARRAY_CONTAINS
.
SELECT
ARRAY_CONTAINS(["apples", "strawberries", "bananas"], "apples") AS b1,
ARRAY_CONTAINS(["apples", "strawberries", "bananas"], "mangoes") AS b2
下面是结果集。Here is the result set.
[{"b1": true, "b2": false}]
以下示例介绍了如何使用 ARRAY_CONTAINS 检查数组内是否存在 JSON 字符串的部分匹配字符串。The following example how to check for a partial match of a JSON in an array using ARRAY_CONTAINS.
SELECT
ARRAY_CONTAINS([{"name": "apples", "fresh": true}, {"name": "strawberries", "fresh": true}], {"name": "apples"}, true) AS b1,
ARRAY_CONTAINS([{"name": "apples", "fresh": true}, {"name": "strawberries", "fresh": true}], {"name": "apples"}) AS b2,
ARRAY_CONTAINS([{"name": "apples", "fresh": true}, {"name": "strawberries", "fresh": true}], {"name": "mangoes"}, true) AS b3
下面是结果集。Here is the result set.
[{
"b1": true,
"b2": false,
"b3": false
}]
备注Remarks
此系统函数将从范围索引中获益。This system function will benefit from a range index.