$anyElementTrue (aggregation)
On this page
Definition
-
$anyElementTrue
-
New in version 2.6.
Evaluates an array as a set and returns
true
if any of the elements aretrue
andfalse
otherwise. An empty array returnsfalse
.$anyElementTrue
has the following syntax:The
<expression>
itself must resolve to an array, separate from the outer array that denotes the argument list. 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.
In addition to the false
boolean value, $anyElementTrue
evaluates as false
the following: null
, 0
, and undefined
values. The $anyElementTrue
evaluates all other values as true
, including non-zero numeric values and arrays.
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:
The following operation uses the $anyElementTrue
operator to determine if the responses
array contains any value that evaluates to true
:
The operation returns the following results: