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

CategoryBitwise
MongoDB Version3.2+
Example Languagemongosh (Shell)

📝 Syntax

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

⚙️ Parameters

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

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

🙏 帮我们做得更好

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

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