$anyElementTrue (aggregation)
Definition
New in version 2.6.
Evaluates an array as a set and returns true
if any of theelements are true
and false
otherwise. An empty arrayreturns false
.
$anyElementTrue
has the following syntax:
- { $anyElementTrue: [ <expression> ] }
The <expression>
itself must resolve to an array, separate fromthe outer array that denotes the argument list. For more informationon expressions, see Expressions.
Behavior
If a set contains a nested array element, $anyElementTrue
does not descendinto the nested array but evaluates the array at top-level.
In addition to the false
boolean value, $anyElementTrue
evaluatesas 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 followingdocuments:
- 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
operatorto determine if the responses
array contains any value thatevaluates to true
:
- db.survey.aggregate(
- [
- { $project: { responses: 1, isAnyTrue: { $anyElementTrue: [ "$responses" ] }, _id: 0 } }
- ]
- )
The operation returns the following results:
- { "responses" : [ true ], "isAnyTrue" : true }
- { "responses" : [ true, false ], "isAnyTrue" : true }
- { "responses" : [ ], "isAnyTrue" : false }
- { "responses" : [ 1, true, "seven" ], "isAnyTrue" : true }
- { "responses" : [ 0 ], "isAnyTrue" : false }
- { "responses" : [ [ ] ], "isAnyTrue" : true }
- { "responses" : [ [ 0 ] ], "isAnyTrue" : true }
- { "responses" : [ [ false ] ], "isAnyTrue" : true }
- { "responses" : [ null ], "isAnyTrue" : false }
- { "responses" : [ undefined ], "isAnyTrue" : false }