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

CategoryBitwise
MongoDB Version3.2+
Example Languagemongosh (Shell)

📝 Syntax

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

⚙️ Parameters

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

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

🙏 帮我们做得更好

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

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