MongoDB $anyElementTrue Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $anyElementTrue?

$anyElementTrue is a MongoDB aggregation expression that looks at an array and returns true if any element is truthy. An empty array returns false.

Remember
$anyElementTrue = OR across array elements

{ $anyElementTrue: [ <array expression> ] }

at least one truthy → true
all falsy / empty   → false

falsy:  false, null, 0, undefined
truthy: non-zero numbers, strings, arrays
nested arrays → truthy (not opened)
UsePrefer
Any array element is true$anyElementTrue
Every array element is true$allElementsTrue
AND / OR of separate expressions$and / $or

How to Use $anyElementTrue in MongoDB

Point it at an array field (or an expression that builds one), then use the boolean result.

  1. Choose an expression stage ($addFields or $project).
  2. Pass an array expression inside the outer argument brackets.
  3. Read the boolean result, or filter with $match + $expr.
  4. Optional: build the array with $map when you need per-item checks first.
PieceRequired?Role
$anyElementTrueYesTrue if any array element is truthy
Array expressionYesMust resolve to an array (e.g. "$responses")
Outer [ ]YesArgument list wrapper around that expression

Pseudocode

Pseudocode
for each document:
    arr = evaluate(arrayExpression)
    if arr is empty:
        result = false
    else:
        result = false
        for each element in arr:
            if element is truthy:   // not false/null/0/undefined
                result = true
                break
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while checking arrays for any true value.

TopicDetail
TypeAggregation expression (not a find filter alone)
Syntax{ $anyElementTrue: [ <array> ] }
Empty arrayfalse
Falsy elementsfalse, null, 0, undefined
Nested arrayCounts as true (not inspected inside)
vs $allElementsTrueany = one; all = every (empty: any false / all true)
Common pattern{ $anyElementTrue: [ "$flags" ] }

Live Preview

Pick a sample array and watch $anyElementTrue decide true or false.

Simulates aggregation truthiness: 0/null/false are falsy; nested arrays are truthy.

Result
responses = [ true, false ]
$anyElementTrue → true

{ isAnyTrue: true }

Worked Walkthrough — Survey Responses

Each survey stores a responses array. We ask: “Did anyone answer true?”

Calculation
Rule:
  $anyElementTrue([ responses ])

[ true, false ]
  true  → truthy  → stop
  $anyElementTrue → true   ✓

[ ]
  no elements
  $anyElementTrue → false  ✗

[ 0 ]
  0 → falsy
  $anyElementTrue → false  ✗

[ [ false ] ]
  nested array → truthy (not opened)
  $anyElementTrue → true   ✓

One truthy element is enough.

MongoDB Examples

We use a small survey collection. First we compute isAnyTrue, then we filter surveys that have at least one truthy response. Insert the sample data once in mongosh.

Example 1 — Sample Data

Imagine five survey rows. Each document has a responses array. We save them in a collection named survey.

mongosh
db.survey.insertMany([
  { _id: 1, responses: [ true ] },
  { _id: 2, responses: [ true, false ] },
  { _id: 3, responses: [] },
  { _id: 4, responses: [ 0 ] },
  { _id: 5, responses: [ [ false ] ] }
]);

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps five documents in survey. Run find() to see them:

mongosh
db.survey.find()

Read those documents like this:

Field in MongoDBExamplePlain English
_id1 … 5Survey id for this lesson
responses[ true, false ]List of answer values to test
Picture
Any-true check (left → right, stop at first truthy):

  [ true, false ]  →  true stops   → true
  [ ]              →  nothing      → false
  [ 0 ]            →  0 is falsy   → false
  [ [ false ] ]    →  array truthy → true

Think: "is there at least one yes?"

How It Works

  1. Insert once. insertMany adds five survey documents.
  2. MongoDB stores arrays as written. Each responses value stays an array of elements.
  3. find() shows the same shape. What you inserted is what you get back—ready for $anyElementTrue next.

Example 2 — Boolean Flag with $anyElementTrue

Goal: add isAnyTrue: true/false for every survey without removing documents.

mongosh
db.survey.aggregate([
  {
    $addFields: {
      isAnyTrue: {
        $anyElementTrue: [ "$responses" ]
      }
    }
  }
]);

How It Works

  1. Read the array. "$responses" is the array expression inside the argument list.
  2. Test each element. Truthy values win immediately; 0 and empty arrays fail.
  3. Nested arrays count as true. Document 5 is true because [ false ] is still an array value.
  4. Keep all rows. $addFields only adds isAnyTrue.

Example 3 — Filter with $match

Same rule, but keep only surveys where at least one response is truthy. Wrap the expression in $expr.

mongosh
db.survey.aggregate([
  {
    $match: {
      $expr: {
        $anyElementTrue: [ "$responses" ]
      }
    }
  }
]);

How It Works

  1. Same stored documents. Still using the five surveys from Example 1.
  2. Same any-true test. Empty and [ 0 ] fail; the others pass.
  3. $expr unlocks the operator. Aggregation expressions inside $match need $expr.
  4. Filter, don’t flag. Only docs 1, 2, and 5 remain—the rows that were isAnyTrue: true.

Edge Cases & Pitfalls

Check these before relying on $anyElementTrue.

empty

Empty array is false

No elements means no truthy value. That differs from $allElementsTrue, where empty is true.

nested

Nested arrays are truthy

[ [ false ] ] returns true. Flatten or map first if you meant to inspect inner values.

falsy

0 and null are false

Only false is not enough to worry about—0, null, and undefined also fail the test.

syntax

Keep the outer brackets

Write { $anyElementTrue: [ "$responses" ] }. The field path sits inside the argument-list array.

Performance Notes

ApproachCostBest for
$anyElementTrue alonePer-document array scanSmall boolean / flag arrays
+ $map then anyMap + scanPer-item conditions first
$match + $exprExpression eval per docFiltering after/with the check

Array scans are cheap on short lists. Filter early with $match when only some documents need the check.

Key Takeaways

  • Role: true if any array element is truthy.
  • Empty: empty array → false.
  • Nested: nested arrays count as true.
  • vs all: use $allElementsTrue when every element must pass.

One line: $anyElementTrue answers “is any value in this array truthy?”—empty means no.

Frequently Asked Questions

$anyElementTrue evaluates an array and returns true if at least one element is truthy. If every element is falsy, or the array is empty, it returns false. Use it inside aggregation stages such as $project and $addFields.
Use { $anyElementTrue: [ <array expression> ] }. The outer brackets are the argument list. The inner expression must resolve to an array.
An empty array returns false—there is no true element.
false, null, 0, and undefined count as false. Non-zero numbers, non-empty strings, and arrays (including nested arrays) count as true.
$anyElementTrue needs only one truthy element. $allElementsTrue needs every element to be truthy. Empty arrays: any → false, all → true.
No. A nested array counts as one truthy value. It does not open the nested array to check its elements.

Next: $arrayElemAt

Return a single array element by its index.

$arrayElemAt tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful