MongoDB $anyElementTrue Operator

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 4 Examples
Array Logic

What You’ll Learn

The $anyElementTrue operator checks whether at least one element in an array evaluates to true. It is ideal for “any item passes” logic — such as finding records with at least one enabled permission, one passing test, or one active flag.

01

Any True Check

At least one must pass.

02

Array Syntax

Pass one array expression.

03

Boolean Arrays

Works on true/false values.

04

With $map

Build conditions per item.

05

Use Cases

Alerts, permissions, QA.

06

Empty Arrays

Empty array returns false.

Definition and Usage

In MongoDB’s aggregation framework, the $anyElementTrue operator evaluates an array and returns true if at least one element is true. For example, [false, true, false] returns true, but [false, false] returns false. This is the opposite strictness of $allElementsTrue, which requires every element to pass.

💡
Beginner Tip

Pair $anyElementTrue with $map when you need to test a custom condition on each array item (for example, “at least one score is above 90”).

📝 Syntax

The $anyElementTrue operator takes one array expression wrapped in an array:

mongosh
{ $anyElementTrue: [ <array expression> ] }

Syntax Rules

  • $anyElementTrue — returns true when at least one element is true.
  • The inner expression must resolve to an array (for example "$flags" or a $map result).
  • An empty array returns false.
  • Use inside stages like $project, $addFields, $match (with $expr), or $set.
  • Available since MongoDB 5.0.

💡 $anyElementTrue vs $allElementsTrue

These operators are often used together. Here is the difference:

$anyElementTrue — at least one element must be true
$allElementsTrue — every element must be true
Array [true, false] → anyElementTrue: true, allElementsTrue: false
Empty array [] → anyElementTrue: false, allElementsTrue: true

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (boolean / array)
Syntax{ $anyElementTrue: [ <array> ] }
InputArray of boolean values (or boolean-like values)
Empty arrayReturns false
SinceMongoDB 5.0
One pass
[
  false,
  true
]

Returns true

All fail
[
  false,
  false
]

Returns false

Empty
[]

Returns false

Field
{
  $anyElementTrue: ["$flags"]
}

From document array

Examples Gallery

Check boolean arrays, filter records with at least one passing flag, and combine $anyElementTrue with $map.

📚 Boolean Flag Arrays

Test whether at least one flag in an array is true using $project.

Sample Input Documents

Suppose you have a checks collection with a flags array on each document:

mongosh
[
  { "_id": 1, "label": "Service A", "flags": [ false, true, false ] },
  { "_id": 2, "label": "Service B", "flags": [ false, false, false ] },
  { "_id": 3, "label": "Service C", "flags": [] }
]

Example 1 — Basic $anyElementTrue on a Field

Check whether at least one flag is true:

mongosh
db.checks.aggregate([
  {
    $project: {
      label: 1,
      flags: 1,
      hasAnyPass: { $anyElementTrue: [ "$flags" ] }
    }
  }
])

How It Works

  • Service A: one flag is true → returns true.
  • Service B: all flags are false → returns false.
  • Service C: empty array → returns false (no true elements).

📈 Practical Patterns

Filter documents, detect high scores, and check permission arrays.

Example 2 — Filter Documents with $match

Return only records where at least one flag is true:

mongosh
db.checks.aggregate([
  {
    $match: {
      $expr: { $anyElementTrue: [ "$flags" ] }
    }
  },
  {
    $project: {
      label: 1,
      status: "has passing check"
    }
  }
])

How It Works

Only Service A has at least one true flag. Services B and C are filtered out.

Example 3 — Combine with $map for Custom Conditions

Check whether at least one score is 90 or above:

mongosh
db.students.aggregate([
  {
    $project: {
      name: 1,
      scores: 1,
      hasHighScore: {
        $anyElementTrue: [
          {
            $map: {
              input: "$scores",
              as: "score",
              in: { $gte: [ "$$score", 90 ] }
            }
          }
        ]
      }
    }
  }
])

How It Works

$map converts each score into a boolean (true if ≥ 90). Then $anyElementTrue checks whether at least one mapped result is true.

Example 4 — At Least One Permission Enabled

Detect users who have any access permission turned on:

mongosh
db.users.aggregate([
  {
    $addFields: {
      permissions: [ "$canRead", "$canWrite", "$canDelete" ]
    }
  },
  {
    $project: {
      username: 1,
      hasAnyAccess: {
        $anyElementTrue: [ "$permissions" ]
      }
    }
  }
])

How It Works

Build a boolean array from permission fields. If at least one is true, the user has some level of access. All-false arrays return false.

🚀 Use Cases

  • Alert triggers — fire when at least one sensor or monitor reports a problem.
  • Partial pass QA — identify runs where at least one test passed (even if not all did).
  • Permission checks — verify a user has at least one enabled capability.
  • Highlight detection — find records with at least one score, tag, or flag above a threshold using $map.

🧠 How $anyElementTrue Works

1

MongoDB resolves the array

The pipeline evaluates the array expression — a field like "$flags" or a $map result.

Input
2

Elements are scanned for true

MongoDB checks each element. The first true value is enough to return true.

Validate
3

Boolean result is returned

If no true elements exist (including empty arrays), the result is false.

Output
=

“At least one” decision

Use the boolean in $match, $cond, or projected status fields.

Conclusion

The $anyElementTrue operator is the right choice for “at least one item passes” logic in MongoDB aggregation pipelines. It complements $allElementsTrue and pairs naturally with $map for custom per-element conditions.

For beginners, remember: one true is enough, an empty array returns false, and you need MongoDB 5.0 or later.

💡 Best Practices

✅ Do

  • Use $map when elements are not already booleans
  • Combine with $match and $expr to filter matching records
  • Handle empty arrays intentionally (they return false)
  • Compare with $allElementsTrue when you need strict “all pass” logic
  • Test arrays with all false, mixed, and all true values

❌ Don’t

  • Confuse $anyElementTrue with $or (different purpose)
  • Assume empty arrays return true (that is $allElementsTrue behavior)
  • Pass a non-array expression without wrapping it correctly
  • Use on MongoDB versions before 5.0
  • Expect all elements to pass when you only need one

Key Takeaways

Knowledge Unlocked

Five things to remember about $anyElementTrue

Use these points when validating arrays in aggregation pipelines.

5
Core concepts
📝 02

Array Syntax

{ $anyElementTrue: [arr] }

Syntax
🛠 03

Works with $map

Custom per-item tests.

Pattern
📋 04

Empty Array

Returns false.

Edge case
🚀 05

MongoDB 5.0+

Requires modern version.

Version

❓ Frequently Asked Questions

$anyElementTrue checks whether at least one element in an array evaluates to true. It returns true when any element is true, and false when no element is true.
The syntax is { $anyElementTrue: [ <array expression> ] }. The expression must resolve to an array of boolean values (or values MongoDB can treat as booleans).
An empty array returns false. There are no true elements, so the condition fails.
$anyElementTrue returns true when at least one element is true. $allElementsTrue returns true only when every element is true.
$anyElementTrue was introduced in MongoDB 5.0 as an aggregation expression operator.

Continue the Operator Series

Move on to $arrayElemAt or review $allElementsTrue for strict all-pass checks.

Next: $arrayElemAt →

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.

5 people found this page helpful