$bitOr (Bitwise OR)

Last updated: 2026-10-09

$bitOr (Bitwise OR)

$bitOr performs a bitwise OR on integers. Available since MongoDB 6.3, it accepts an array of integers (or a single number). It is used to combine flag bits or merge permissions, such as packing several permission bits into one integer. Any null or missing argument yields null; operands must be int32 or int64, and other types throw. Where $bitAnd reads bits, $bitOr sets them, so together they cover bit-mask read and write.

CategoryMath
MongoDB Version6.3+
Example Languagemongosh (Shell)

📝 Syntax

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

⚙️ Parameters

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

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

▶ Example

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

// $bitOr: merge read and write permission bits
db.users.aggregate([
  { $project: {
      combined: { $bitOr: ['$permRead', '$permWrite'] }
  } }
])

Output:

{ '_id': 1, 'combined': 6 }
{ '_id': 2, 'combined': 3 }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QHow do $bitOr and $bitAnd differ?
A$bitOr sets a bit to 1 when either operand has it; $bitAnd keeps it only when both do.
QCan $bitOr merge permissions?
AYes, OR the permission bits together to merge them into one integer.
QWhat does $bitOr return for null?
AAny null or missing argument makes the result null.
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%

🙏 帮我们做得更好

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

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