$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.

CategoryBitwise
MongoDB Version3.2+
Example Languagemongosh (Shell)

📝 Syntax

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

⚙️ Parameters

fieldThe integer or BinData field to match.
bitmaskAn 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

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.
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%

🙏 帮我们做得更好

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

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