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.
Fundamentals
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”).
Foundation
📝 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
Build a temporary boolean array from individual permission fields, then test whether every permission is true. Users missing any permission get false.
Applications
🚀 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $allElementsTrue
Use these points when validating arrays in aggregation pipelines.
5
Core concepts
✅01
All Must Pass
No false elements allowed.
Purpose
📝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.