$anyElementTrue (Check Any Element Is Truthy)
Last updated: 2026-10-09
$anyElementTrue (Check Any Element Is Truthy)
$anyElementTrue returns true when at least one element of an array is truthy. An empty array returns false. Typical uses: checking whether a user has any shipping address or an order has any active discount. It is the dual of $allElementsTrue — true if any element is truthy. Truthiness follows the same rules (0, empty string, null, false are falsy).
| Category | Set, Condition & Conversion |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $anyElementTrue: [ <array> ] }
⚙️ Parameters
| array | The array to check. |
|---|
Returns:true when any element is truthy; an empty array returns false
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $anyElementTrue: is any VIP flag set
db.users.aggregate([
{ $project: {
isVip: { $anyElementTrue: ['$vipFlags'] }
} }
])
Output:
{ '_id': 1, 'isVip': true }
{ '_id': 2, 'isVip': false }
📌 Notes
- An empty array returns false
- Pair with $allElementsTrue for complete all-of/any-of logic
- Falsy values are 0, '', null and false
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat does an empty array return?
AIt returns false, the opposite of $allElementsTrue.
QHow does it relate to $allElementsTrue?
AThey are duals: one is true if any element is truthy, the other only if all are.
QCan I pass an array of boolean fields?
AYes for boolean arrays; map out the field first for object arrays.