Definition定义
$anyElementTrueEvaluates an array as a set and returns将数组作为一个集合进行求值,如果任何元素为trueif any of the elements aretrueandfalseotherwise. An empty array returnsfalse.true,则返回true,否则返回false。空数组返回false。$anyElementTruehas 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还将以下值计算为false:null、0和undefined值。The $anyElementTrue evaluates all other values as true, including non-zero numeric values and arrays.$anyElementTrue将所有其他值计算为true,包括非零数值和数组。
{ $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: 1istruebecause the element inside theresponsesarray evaluates astrue._id: 1的文档为true,因为responses数组中的元素计算结果为true。Documents with具有_id: 2and_id: 4aretruebecause at least one element inside theresponsesarray evaluates astrue._id: 2和_id: 4的文档为true,因为responses数组中至少有一个元素的计算结果为true。Documents with具有_id: 6,_id: 7, and_id: 8aretruebecause theresponsesarray, which is the array that$anyElementTrueevaluated for the operation, contains a nested array, which$anyElementTruealways evaluates astrue._id:6、_id:7和_id:8的文档为true,因为响应数组($anyElementTrue为操作计算的数组)包含一个嵌套数组,$anyElementTrue总是将其计算为true。