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.
Fundamentals
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”).
Foundation
📝 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
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.
Applications
🚀 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $anyElementTrue
Use these points when validating arrays in aggregation pipelines.
5
Core concepts
✔01
Any True
One true is enough.
Purpose
📝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.