$allElementsTrue (Check All Elements Are Truthy)
Last updated: 2026-10-09
$allElementsTrue (Check All Elements Are Truthy)
$allElementsTrue returns true only if every element of an array is truthy. An empty array returns true, following the universal-quantification convention. Typical uses: verifying that every item of an order is in stock or that a user checked all required options. It takes a single array argument, and elements follow MongoDB truthiness: 0, empty string, null and false are falsy, everything else is truthy. It is the dual of $anyElementTrue.
| Category | Set, Condition & Conversion |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $allElementsTrue: [ <array> ] }
⚙️ Parameters
| array | The array to check; elements are judged by truthiness. |
|---|
Returns:true when every element is truthy; an empty array returns true
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $allElementsTrue: check every item is in stock
db.orders.aggregate([
{ $project: {
allInStock: { $allElementsTrue: ['$inStockFlags'] }
} }
])
Output:
{ '_id': 1, 'allInStock': true }
{ '_id': 2, 'allInStock': false }
📌 Notes
- An empty array returns true, counter-intuitively; combine with $size to detect emptiness
- Falsy values are 0, '', null and false; everything else is truthy
- Accepts arrays only; non-array arguments throw
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat does an empty array return?
AIt returns true by universal quantification; use $size for emptiness checks.
QWhich values are falsy?
A0, empty string, null and false; anything else, including the string '0', is truthy.
QCan it check fields of object arrays?
AExtract the field into a boolean array with $map first, then pass it in.