MongoDB $anyElementTrue

In MongoDB, the $anyElementTrue aggregation pipeline operator evaluates an array as a set and returns true if any of the elements are true.

If none of the elements is true, then it returns false.

An array’s element is true if it’s not false, null, 0, or undefined.

Example

Suppose we have a collection with the following document:

{ "_id" : 1, "data" : [ 1, 2, 3 ] }

This document contains an array.

We can run the following query with $anyElementTrue to find out whether or not the array contains any elements that evaluate to true:

db.test.aggregate(
   [
     { $project: { 
        _id: 0,
        anyElementTrue: { $anyElementTrue: [ "$data" ] } } 
         }
   ]
)

Result:

{ "anyElementTrue" : true }

In this case, all of the array elements evaluate to true (i.e. they’re not false, null, 0, or undefined), and so we get a result of true.

When No Element is True

Let’s add the following document to the collection:

{ "_id" : 2, "data" : [ false, undefined, 0, null ] }

And let’s run $anyElementTrue against that document:

db.test.aggregate(
  [
    { $match: { _id: 2 } },
    { $project: { 
      _id: 0,
      anyElementTrue: { $anyElementTrue: [ "$data" ] } } 
        }
  ]
)

Result:

{ "anyElementTrue" : false }

As we can see here, $anyElementTrue evaluates to false whenever an array contains false, null, 0, or undefined values.

When the Array Contains Both True and False

Let’s add the following document to the collection:

{ "_id" : 3, "data" : [ true, false ] }

And let’s run $anyElementTrue against that document:

db.test.aggregate(
  [
    { $match: { _id: 3 } },
    { $project: { 
      _id: 0,
      anyElementTrue: { $anyElementTrue: [ "$data" ] } } 
        }
  ]
)

Result:

{ "anyElementTrue" : true }

We get true, even though there’s another element that is false. This is to be expected, because $anyElementTrue returns true whenever there is at least one element that is true, regardless of how many other elements are false.

Empty Arrays

Empty arrays return true.

Suppose we add the following document to our collection:

{ "_id" : 4, "data" : [ ] }

This document contains an empty array.

Now let’s run $anyElementTrue again:

db.test.aggregate(
  [
    { $match: { _id: 4 } },
    { $project: { 
      _id: 0,
      anyElementTrue: { $anyElementTrue: [ "$data" ] } } 
        }
  ]
)

Result:

{ "anyElementTrue" : false }

It returns false, because an empty array is not false.

Nested Arrays

The $anyElementTrue operator does not descend into any nested arrays. It evaluates the array at top-level.

Therefore, whether the nested array contains elements that are true or false is irrelevant to $anyElementTrue. As far as $anyElementTrue is concerned, the nested array is the element, and therefore true.

To demonstrate what I mean, suppose we insert the following documents:

{ "_id" : 5, "data" : [ false, [ false ] ] }
{ "_id" : 6, "data" : [ false, false ] }

Now let’s run $anyElementTrue against those two documents:

db.test.aggregate(
  [
    { $match: {_id: { $in: [5,6] }} },
    { $project: { 
      anyElementTrue: { $anyElementTrue: [ "$data" ] } } 
        }
  ]
)

Result:

{ "_id" : 5, "anyElementTrue" : true }
{ "_id" : 6, "anyElementTrue" : false }

We can see that the first document returned true and the second returned false.

This is because, the array in the first document contains a nested array, which is enough to return true, regardless of its contents.

The second document doesn’t contain a nested array – it only contains two false values – and it therefore evaluates to false.