$bitsAllClear (All Bits Clear)
Last updated: 2026-10-09
$bitsAllClear (All Bits Clear)
$bitsAllClear matches documents where all bits at the given positions are 0. `{ flags: { $bitsAllClear: 3 } }` (binary 11) matches documents where bits 0 and 1 are both 0. Useful for checking that a group of flags is fully off. Works on integers and BinData only.
| Category | Bitwise |
|---|---|
| MongoDB Version | 3.2+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ <field>: { $bitsAllClear: <bitmask or position array> } }
⚙️ Parameters
| field | The integer or BinData field to match. |
|---|---|
| bitmask | An integer bitmask or position array, e.g. 3 or [0, 1]. |
Returns:Cursor of matching documents
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $bitsAllClear: bits 0 and 1 are both 0
// 3 in binary is 11
db.users.find({ flags: { $bitsAllClear: 3 } })
// Equivalent: array of positions
db.users.find({ flags: { $bitsAllClear: [0, 1] } })
Output:
// Returns users whose flags have bits 0 and 1 both clear
📌 Notes
- Complement of $bitsAllSet: all-0 vs all-1
- Integers (int, long) and BinData only
- Also checks a single bit: { $bitsAllClear: [n] }
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QHow do I check that a bit is 0?
AUse $bitsAllClear: [position], e.g. [2] tests bit 2 is 0.
QWhat is the difference between $bitsAllClear and $bitsAnyClear?
A$bitsAllClear needs every listed bit to be 0; $bitsAnyClear needs only one.
QWhich types are supported?
AOnly int, long and BinData.