ARRAY_CONTAINS - Cosmos DB 中的查询语言(在 Azure 和 Fabric 中)

ARRAY_CONTAINS 函数返回一个布尔值,该值指示数组是否包含指定的值。 可以使用函数中的布尔表达式来检查对象的部分或完全匹配。

Syntax

ARRAY_CONTAINS(<array_expr>, <expr> [, <bool_expr>])

Arguments

Description
array_expr 数组表达式。
expr 要搜索数组中的表达式。
bool_expr 一个布尔表达式,指示搜索是应检查部分匹配(true)还是完全匹配(false)。 如果未指定,默认值为 false

返回类型

返回布尔值。

例子

本部分包含有关如何使用此查询语言构造的示例。

数组包含示例

在此示例中,该 ARRAY_CONTAINS 函数用于检查数组中是否存在值和对象。

SELECT VALUE {
  containsItem: ARRAY_CONTAINS(["coats", "jackets", "sweatshirts"], "coats"),
  missingItem: ARRAY_CONTAINS(["coats", "jackets", "sweatshirts"], "hoodies"),
  containsFullMatchObject: ARRAY_CONTAINS([{ category: "shirts", color: "blue" }], { category: "shirts", color: "blue" }),
  missingFullMatchObject: ARRAY_CONTAINS([{ category: "shirts", color: "blue" }], { category: "shirts" }),
  containsPartialMatchObject: ARRAY_CONTAINS([{ category: "shirts", color: "blue" }], { category: "shirts" }, true),
  missingPartialMatchObject: ARRAY_CONTAINS([{ category: "shirts", color: "blue" }], { category: "shorts", color: "blue" }, true)
}
[
  {
    "containsItem": true,
    "missingItem": false,
    "containsFullMatchObject": true,
    "missingFullMatchObject": false,
    "containsPartialMatchObject": true,
    "missingPartialMatchObject": false
  }
]

注解

  • 此函数受益于范围索引的使用。 有关详细信息,请参阅 范围索引