$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.
Provide an array of boolean results (a field, or values from $map).
Wrap that array expression in { $allElementsTrue: [ ... ] }.
Use the boolean result for filtering, scoring, or a pass/fail field.
Piece
Required?
Role
$allElementsTrue
Yes
True only if every element is true
Array expression
Yes
Must resolve to an array (outer [] wraps it)
$map
No
Turn 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 }
Reference
Cheat Sheet
Keep these facts handy while writing array boolean checks.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $allElementsTrue: [ <array> ] }
Empty array
Returns true
False-like
false, 0, null
Sibling
$anyElementTrue = at least one true
Map pattern
$allElementsTrue: [ { $map: ... } ]
Sample check
[true, true, true] → true
Try it
Live Preview
Watch $allElementsTrue decide pass/fail from a checklist array.
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
Code
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.
Start from scores. Still using [80, 90, 75] from Example 1.
Map to booleans.$map + $gte turns them into [true, true, true].
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.
Analysis
Performance Notes
Approach
Cost
Best for
$allElementsTrue on a field
Per-document, short arrays
Ready-made boolean arrays
$map + $allElementsTrue
O(array length) per doc
Scores/thresholds → pass/fail
Filter with $match after
Still scans matching docs
Keep only fully-passed rows
Array walks are cheap for small checklists. Filter early with $match when only some documents need the check.
Remember
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.