MongoDB $allElementsTrue Operator

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

What You’ll Learn

The $allElementsTrue operator checks whether every element in an array evaluates to true. It is ideal for validation checklists, permission flags, test results, and any “all must pass” logic inside aggregation pipelines.

01

All True Check

Verify every element passes.

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

QA, permissions, validation.

06

Empty Arrays

Empty array returns true.

Definition and Usage

In MongoDB’s aggregation framework, the $allElementsTrue operator evaluates an array and returns true if no element is false. For example, [true, true, true] returns true, but [true, false, true] returns false. This is useful when you store boolean flags or pass/fail results as arrays in documents.

💡
Beginner Tip

Pair $allElementsTrue with $map when you need to test a condition on each item in an array (for example, “every score is at least 60”).

📝 Syntax

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

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

Syntax Rules

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

💡 $allElementsTrue vs $anyElementTrue

These two operators are often confused. Here is the difference:

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

⚡ Quick Reference

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

Returns true

One fail
[
  true,
  false
]

Returns false

Empty
[]

Returns true

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

From document array

Examples Gallery

Check boolean arrays, validate checklists, and combine $allElementsTrue with $map for custom conditions.

📚 Boolean Flag Arrays

Start with a collection that stores boolean arrays and test whether all flags are true.

Sample Input Documents

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

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

Example 1 — Basic $allElementsTrue on a Field

Check whether every flag in the array is true:

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

How It Works

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

📈 Practical Patterns

Filter passing records, validate checklists, and evaluate custom conditions with $map.

Example 2 — Filter Documents Where All Checks Pass

Use $match with $expr to return only fully validated records:

mongosh
db.checks.aggregate([
  {
    $match: {
      $expr: { $allElementsTrue: [ "$flags" ] }
    }
  },
  {
    $project: {
      label: 1,
      status: "approved"
    }
  }
])

How It Works

Deployment B is excluded because one flag is false. This pattern is common in QA pipelines where only fully passing records should proceed.

Example 3 — Combine with $map for Custom Conditions

Check whether every score in an array is at least 60:

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

How It Works

$map converts each score into a boolean (true if ≥ 60). Then $allElementsTrue checks whether every mapped result is true.

Example 4 — Permission Flags on a User Record

Verify that a user has all required permissions enabled:

mongosh
db.users.aggregate([
  {
    $addFields: {
      requiredPerms: [ "$canRead", "$canWrite", "$canDelete" ]
    }
  },
  {
    $project: {
      username: 1,
      hasFullAccess: {
        $allElementsTrue: [ "$requiredPerms" ]
      }
    }
  }
])

How It Works

Build a temporary boolean array from individual permission fields, then test whether every permission is true. Users missing any permission get false.

🚀 Use Cases

  • QA and testing — confirm all test cases in an array passed before approving a release.
  • Permission checks — verify every required access flag is enabled on a user or role.
  • Form validation — ensure all checklist items are marked complete.
  • Data quality — validate that every item in an array meets a condition using $map.

🧠 How $allElementsTrue Works

1

MongoDB resolves the array

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

Input
2

Each element is evaluated

MongoDB checks every element. If any element is false, the operator stops and returns false.

Validate
3

Boolean result is returned

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

Output
=

Pass / fail decision

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

Conclusion

The $allElementsTrue operator is a clean way to express “every item must pass” logic in MongoDB aggregation pipelines. It works directly on boolean arrays and pairs naturally with $map when you need custom per-element conditions.

For beginners, remember: one false fails the check, an empty array passes, 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 passing records
  • Handle empty arrays intentionally (they return true)
  • Compare with $anyElementTrue when you need “at least one” logic
  • Test with mixed true/false arrays during development

❌ Don’t

  • Confuse $allElementsTrue with $and (different purpose)
  • Pass a non-array expression without wrapping it correctly
  • Assume empty arrays should fail (they return true by default)
  • Use on MongoDB versions before 5.0
  • Forget that one false value fails the entire check

Key Takeaways

Knowledge Unlocked

Five things to remember about $allElementsTrue

Use these points when validating arrays in aggregation pipelines.

5
Core concepts
📝 02

Array Syntax

{ $allElementsTrue: [arr] }

Syntax
🛠 03

Works with $map

Custom per-item tests.

Pattern
📋 04

Empty Array

Returns true.

Edge case
🚀 05

MongoDB 5.0+

Requires modern version.

Version

❓ Frequently Asked Questions

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

Continue the Operator Series

Move on to $and for combining multiple boolean expressions.

Next: $and →

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