$bitsAllSet (All Bits Set)
Last updated: 2026-10-09
$bitsAllSet (All Bits Set)
$bitsAllSet matches documents where all bits at the given positions are set to 1. `{ flags: { $bitsAllSet: 5 } }` (binary 101) matches documents where bit 0 and bit 2 are both 1. Bits are numbered from the least significant (0). Works on integers (int, long) and BinData; ideal for bitmask permissions.
| Category | Bitwise |
|---|---|
| MongoDB Version | 3.2+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ <field>: { $bitsAllSet: <bitmask or position array> } }
⚙️ Parameters
| field | The integer or BinData field to match. |
|---|---|
| bitmask | An integer bitmask or position array, e.g. 5 or [0, 2]. |
Returns:Cursor of matching documents
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $bitsAllSet: bits 0 and 2 are both set
// 5 in binary is 101
db.users.find({ flags: { $bitsAllSet: 5 } })
// Equivalent: array of positions
db.users.find({ flags: { $bitsAllSet: [0, 2] } })
Output:
// Returns users whose flags have bits 0 and 2 both set
📌 Notes
- Bits are numbered from the least significant (0)
- Works on integers (int, long) and BinData only
- Unlike $bitsAnySet, every listed bit must be 1
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $bitsAllSet and $bitsAnySet?
A$bitsAllSet requires every listed bit to be 1; $bitsAnySet needs only one.
QCan I use a position array instead of a bitmask?
AYes, { $bitsAllSet: [0, 2] } equals { $bitsAllSet: 5 }.
QDoes $bitsAllSet support floats?
ANo, only int, long and BinData.