$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.

CategorySet, Condition & Conversion
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $allElementsTrue: [ <array> ] }

⚙️ Parameters

arrayThe 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏