Database Manual / Reference / Query Language / Expressions

$anyElementTrue (expression operator)(表达式运算符)

Definition定义

$anyElementTrue

Evaluates an array as a set and returns true if any of the elements are true and false otherwise. An empty array returns false.将数组作为一个集合进行求值,如果任何元素为true,则返回true,否则返回false。空数组返回false

$anyElementTrue has the following syntax:具有以下语法:

{ $anyElementTrue: [ <expression> ] }

The <expression> itself must resolve to an array, separate from the outer array that denotes the argument list. <expression>本身必须解析为一个数组,与表示参数列表的外部数组分开。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

If a set contains a nested array element, $anyElementTrue does not descend into the nested array but evaluates the array at top-level.如果一个集合包含嵌套数组元素,$anyElementTrue不会下降到嵌套数组中,而是在顶级计算数组。

In addition to the false boolean value, $anyElementTrue evaluates as false the following: null, 0, and undefined values. 除了false布尔值外,$anyElementTrue还将以下值计算为falsenull0undefined值。The $anyElementTrue evaluates all other values as true, including non-zero numeric values and arrays.$anyElementTrue将所有其他值计算为true,包括非零数值和数组。

Example示例Result结果
{ $anyElementTrue: [ [ true, false ] ] }true
{ $anyElementTrue: [ [ [ false ] ] ] }true
{ $anyElementTrue: [ [ null, false, 0 ] ] }false
{ $anyElementTrue: [ [ ] ] }false

Example示例

Create an example collection named survey with the following documents:使用以下文档创建一个名为survey的示例集合:

db.survey.insertMany( [
{ _id: 1, responses: [ true ] },
{ _id: 2, responses: [ true, false ] },
{ _id: 3, responses: [ ] },
{ _id: 4, responses: [ 1, true, "seven" ] },
{ _id: 5, responses: [ 0 ] },
{ _id: 6, responses: [ [ ] ] },
{ _id: 7, responses: [ [ 0 ] ] },
{ _id: 8, responses: [ [ false ] ] },
{ _id: 9, responses: [ null ] },
{ _id: 10, responses: [ undefined ] }
] )

The following operation uses the $anyElementTrue operator to determine if the responses array contains any value that evaluates to true:以下操作使用$anyElementTrue运算符来确定responses数组是否包含任何计算结果为true的值:

db.survey.aggregate(
[
{ $project: { responses: 1, isAnyTrue: { $anyElementTrue: [ "$responses" ] }, _id: 1 } }
]
)
[
{ _id: 1, responses: [ true ], isAnyTrue: true },
{ _id: 2, responses: [ true, false ], isAnyTrue: true },
{ _id: 3, responses: [], isAnyTrue: false },
{ _id: 4, responses: [ 1, true, 'seven' ], isAnyTrue: true },
{ _id: 5, responses: [ 0 ], isAnyTrue: false },
{ _id: 6, responses: [ [] ], isAnyTrue: true },
{ _id: 7, responses: [ [ 0 ] ], isAnyTrue: true },
{ _id: 8, responses: [ [ false ] ], isAnyTrue: true },
{ _id: 9, responses: [ null ], isAnyTrue: false },
{ _id: 10, responses: [ null ], isAnyTrue: false }
]

In the results:在结果中:

  • Document with _id: 1 is true because the element inside the responses array evaluates as true.具有_id: 1的文档为true,因为responses数组中的元素计算结果为true
  • Documents with _id: 2 and _id: 4 are true because at least one element inside the responses array evaluates as true.具有_id: 2_id: 4的文档为true,因为responses数组中至少有一个元素的计算结果为true
  • Documents with _id: 6, _id: 7, and _id: 8 are true because the responses array, which is the array that $anyElementTrue evaluated for the operation, contains a nested array, which $anyElementTrue always evaluates as true.具有_id:6_id:7_id:8的文档为true,因为响应数组($anyElementTrue为操作计算的数组)包含一个嵌套数组,$anyElementTrue总是将其计算为true