MongoDB $allElementsTrue Operator

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

What Is $allElementsTrue?

$allElementsTrue is a MongoDB aggregation expression that returns true when every element in an array evaluates to true. One false element makes the whole result false. An empty array returns true.

Remember
$allElementsTrue ≈ Array.every(Boolean)

{ $allElementsTrue: [ <array expression> ] }

[true, true, true]  → true
[true, false, true] → false
[]                  → true  (no false items)

null / missing array → null
UsePrefer
Every flag / check is true$allElementsTrue
At least one element is true$anyElementTrue
Combine separate boolean fields$and

How to Use $allElementsTrue in MongoDB

Start with an array of booleans (or build one with $map), then ask if every value is true.

  1. Choose an expression stage ($addFields or $project).
  2. Provide an array of boolean results (a field, or values from $map).
  3. Wrap that array expression in { $allElementsTrue: [ ... ] }.
  4. Use the boolean result for filtering, scoring, or a pass/fail field.
PieceRequired?Role
$allElementsTrueYesTrue only if every element is true
Array expressionYesMust resolve to an array (outer [] wraps it)
$mapNoTurn scores/values into booleans first

Pseudocode

Pseudocode
for each document:
    arr = evaluate(array expression)
    if arr is null or missing:
        result = null
    else if arr is empty:
        result = true
    else:
        result = true
        for each item in arr:
            if item is false-like:
                result = false
                break
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while writing array boolean checks.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $allElementsTrue: [ <array> ] }
Empty arrayReturns true
False-likefalse, 0, null
Sibling$anyElementTrue = at least one true
Map pattern$allElementsTrue: [ { $map: ... } ]
Sample check[true, true, true] → true

Live Preview

Watch $allElementsTrue decide pass/fail from a checklist array.

Simulates { $allElementsTrue: [ checks ] }.

Result
checks = [true, true, true]
$allElementsTrue → true

{ all_passed: true }

Worked Walkthrough — Deploy Checklist

A deploy has three checks: tests, build, and review. All three must be true before go-live.

Calculation
Document:
  { task: "Deploy", checks: [true, true, true] }

Step through each item:
  checks[0] = true
  checks[1] = true
  checks[2] = true

No false values → $allElementsTrue → true

If checks were [true, false, true]:
  checks[1] = false → result false

MongoDB Examples

We use one deploy checklist. First we test a boolean array, then build booleans from scores with $map. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine one deploy task with three boolean checks and three quiz scores. We save both arrays in a collection named checklists.

mongosh
db.checklists.insertOne({
  _id: 1,
  task: "Deploy",
  checks: [true, true, true],
  scores: [80, 90, 75]
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in checklists. Run find() to see it:

mongosh
db.checklists.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id (row number for this lesson)
task"Deploy"What we are checking
checks[true, true, true]Three pass/fail flags
scores[80, 90, 75]Three quiz scores (for Example 3)
Picture
Deploy checklist:

  [1] tests   ✓ true
  [2] build   ✓ true
  [3] review  ✓ true

Question: are ALL checks true?
  → $allElementsTrue → true

How It Works

  1. Insert once. insertOne adds a single checklist document to checklists.
  2. MongoDB stores arrays as written. checks holds booleans; scores holds numbers.
  3. find() shows the same shape. What you inserted is what you get back—ready for $allElementsTrue next.

Example 2 — Boolean Array

Goal: set all_passed to whether every value in checks is true.

mongosh
db.checklists.aggregate([
  {
    $addFields: {
      all_passed: {
        $allElementsTrue: ["$checks"]
      }
    }
  }
]);

How It Works

  1. Read the stored array. MongoDB already has checks: [true, true, true] from Example 1.
  2. Ask “all true?”. $allElementsTrue: ["$checks"] walks every element.
  3. No false values. Result is true, stored as all_passed via $addFields.

Example 3 — Scores with $map

Same document, but scores are numbers. First turn each score into “≥ 70?”, then require every result to be true.

mongosh
db.checklists.aggregate([
  {
    $addFields: {
      all_scores_pass: {
        $allElementsTrue: [
          {
            $map: {
              input: "$scores",
              as: "s",
              in: { $gte: ["$$s", 70] }
            }
          }
        ]
      }
    }
  }
]);

How It Works

  1. Start from scores. Still using [80, 90, 75] from Example 1.
  2. Map to booleans. $map + $gte turns them into [true, true, true].
  3. Require every pass. $allElementsTrue on that array → true (all scores ≥ 70).

Edge Cases & Pitfalls

Check these before using $allElementsTrue in production pipelines.

empty

Empty array is true

No elements means no failures. If empty should fail, also check array size with $size.

wrap

Keep the outer array

Syntax is { $allElementsTrue: [ <arrayExpr> ] }. Forgetting the outer brackets causes errors.

truthy

Not only strict booleans

0 and null count as false; non-zero numbers count as true. Prefer real true/false values.

sibling

Do not confuse with $anyElementTrue

“All” vs “any” is an easy mix-up. Use $anyElementTrue when one success is enough.

Performance Notes

ApproachCostBest for
$allElementsTrue on a fieldPer-document, short arraysReady-made boolean arrays
$map + $allElementsTrueO(array length) per docScores/thresholds → pass/fail
Filter with $match afterStill scans matching docsKeep only fully-passed rows

Array walks are cheap for small checklists. Filter early with $match when only some documents need the check.

Key Takeaways

  • Role: true only if every array element is true.
  • Empty: [] returns true.
  • Pattern: $map first when you have numbers, not booleans.
  • Sibling: use $anyElementTrue for “at least one”.

One line: $allElementsTrue answers “did every item pass?”—true for all-true arrays (and empty ones).

Frequently Asked Questions

$allElementsTrue returns true when every element in an array evaluates to true. If any element is false, it returns false. Use it inside aggregation stages such as $project and $addFields.
Use { $allElementsTrue: [ <array expression> ] }. The outer brackets wrap one expression that must resolve to an array.
An empty array returns true—there are no false elements, so the check passes.
$allElementsTrue needs every element true. $anyElementTrue needs at least one element true.
Yes. MongoDB treats 0, null, and false as false; other values (including non-zero numbers and non-empty strings) count as true. Prefer real booleans for clarity.
Use $map to turn each score into a boolean (for example score >= 70), then pass that array to $allElementsTrue.

Next: $and

Combine multiple boolean expressions in an aggregation pipeline.

$and tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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