- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $allElementsTrue (aggregation)
$allElementsTrue (aggregation)¶
On this page
Definition¶
-
$allElementsTrue
¶ Evaluates an array as a set and returns
true
if no element in the array isfalse
. Otherwise, returnsfalse
. An empty array returnstrue
.$allElementsTrue
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, $allElementsTrue
does not descend
into the nested array but evaluates the array at top-level.
In addition to the false
boolean value, $allElementsTrue
evaluates
as false
the following: null
, 0
, and undefined
values. The $allElementsTrue
evaluates all other values as true
,
including non-zero numeric values and arrays.
Example | Result | |
---|---|---|
{ $allElementsTrue: [ [ true, 1, "someString" ] ] } |
true |
|
{ $allElementsTrue: [ [ [ false ] ] ] } |
true |
|
{ $allElementsTrue: [ [ ] ] } |
true |
|
{ $allElementsTrue: [ [ null, false, 0 ] ] } |
false |
Example¶
Create an example collection named survey
with the following
documents:
The following operation uses the $allElementsTrue
operator to determine if the responses
array only contains values
that evaluate to true
:
The operation returns the following results: