$bitsAnySet (Any Bit Set)

Last updated: 2026-10-09

$bitsAnySet (Any Bit Set)

$bitsAnySet matches documents where at least one of the bits at the given positions is 1. `{ flags: { $bitsAnySet: 12 } }` (binary 1100) matches documents where bit 2 or bit 3 is set. Good for any-of permissions. Integers and BinData only.

CategoryBitwise
MongoDB Version3.2+
Example Languagemongosh (Shell)

📝 Syntax

{ <field>: { $bitsAnySet: <bitmask or position array> } }

⚙️ Parameters

fieldThe integer or BinData field to match.
bitmaskAn integer bitmask or position array, e.g. 12 or [2, 3].

Returns:Cursor of matching documents

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $bitsAnySet: bit 2 or bit 3 is set
// 12 in binary is 1100
db.users.find({ flags: { $bitsAnySet: 12 } })

// Array of positions
db.users.find({ flags: { $bitsAnySet: [2, 3] } })

Output:

// Returns users whose flags have bit 2 or bit 3 set

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QWhat is $bitsAnySet good for?
AAny-flag-on checks, e.g. pass if the user holds any role permission.
QHow do I choose between $bitsAnySet and $bitsAllSet?
AAny-of uses AnySet; all-of uses AllSet.
QCan position arrays and bitmasks be mixed?
AThey are equivalent; pick one form.
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%

🙏 帮我们做得更好

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

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