$bitsAnyClear (Any Bit Clear)
Last updated: 2026-10-09
$bitsAnyClear (Any Bit Clear)
$bitsAnyClear matches documents where at least one of the bits at the given positions is 0. `{ flags: { $bitsAnyClear: 7 } }` (binary 111) matches documents where bit 0, 1 or 2 is clear. It is more permissive than $bitsAllClear. Integers and BinData only.
| Category | Bitwise |
|---|---|
| MongoDB Version | 3.2+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ <field>: { $bitsAnyClear: <bitmask or position array> } }
⚙️ Parameters
| field | The integer or BinData field to match. |
|---|---|
| bitmask | An integer bitmask or position array, e.g. 7 or [0, 1, 2]. |
Returns:Cursor of matching documents
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $bitsAnyClear: at least one of bits 0-2 is 0
// 7 in binary is 111
db.users.find({ flags: { $bitsAnyClear: 7 } })
// Array of positions
db.users.find({ flags: { $bitsAnyClear: [0, 1, 2] } })
Output:
// Returns users whose flags have at least one of bits 0-2 clear
📌 Notes
- Unlike $bitsAllClear, a single clear bit suffices
- Good for has-any-missing-flag checks
- Only int, long and BinData
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $bitsAnyClear and $bitsAllClear?
A$bitsAnyClear matches on one clear bit; $bitsAllClear needs every listed bit clear.
QHow do I check that a bit is unset?
A{ $bitsAnyClear: [2] } tests bit 2 is 0.
QDoes it support floats?
ANo, only int, long and BinData.