$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.
| Category | Bitwise |
|---|---|
| MongoDB Version | 3.2+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ <field>: { $bitsAnySet: <bitmask or position array> } }
⚙️ Parameters
| field | The integer or BinData field to match. |
|---|---|
| bitmask | An 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
- Unlike $bitsAllSet, a single set bit suffices
- Good for any-permission-allowed checks
- Only int, long and BinData
🏷️ Related Tags
💡 Related Cases
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.