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

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

📝 Syntax

{ $anyElementTrue: [ <array> ] }

⚙️ Parameters

arrayThe 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

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

🙏 帮我们做得更好

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

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