$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.
| Category | Math |
|---|---|
| MongoDB Version | 6.3+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $bitOr: [ <integer1>, <integer2>, ... ] }
⚙️ Parameters
| integer | int32 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
- Only int32 and int64 are supported
- Any null or missing argument makes the result null
- Pair with $bitAnd for full bit-mask read and write
🏷️ Related Tags
💡 Related Cases
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.