$bitAnd (Bitwise AND)

Last updated: 2026-10-09

$bitAnd (Bitwise AND)

$bitAnd performs a bitwise AND on integers. Available since MongoDB 6.3, it accepts an array of integers (or a single number). It is used for permission-bit checks, flag filtering, and subnet-mask math, such as testing whether a permission integer contains a given bit. A null or missing argument yields null; operands must be int32 or int64, and doubles, Decimals, or other types throw. It belongs to the bitwise family with $bitOr, $bitXor, and $bitNot.

CategoryMath
MongoDB Version6.3+
Example Languagemongosh (Shell)

📝 Syntax

{ $bitAnd: [ <integer1>, <integer2>, ... ] }

⚙️ Parameters

integerint32 or int64 integers, or expressions resolving to them; at least two.

Returns:The bitwise AND of all operands; null when null or missing

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $bitAnd: check whether permission bit 4 is set
db.users.aggregate([
  { $project: {
      hasWrite: { $ne: [{ $bitAnd: ['$permissions', 4] }, 0] }
  } }
])

Output:

{ '_id': 1, 'hasWrite': true }
{ '_id': 2, 'hasWrite': false }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QDoes $bitAnd support floating-point numbers?
ANo — only int32 and int64; floating-point input throws.
QHow do I check whether a permission bit is on?
AAND the value with the target bit; a non-zero result means the bit is set.
QSince which version is $bitAnd available?
AAvailable since MongoDB 6.3.
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%

🙏 帮我们做得更好

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

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