$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.
| Category | Math |
|---|---|
| MongoDB Version | 6.3+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $bitAnd: [ <integer1>, <integer2>, ... ] }
⚙️ Parameters
| integer | int32 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
- Only int32 and int64 are supported; floating-point values throw
- The result type matches the operands
- Compare the result with 0 to test whether a bit is set
🏷️ Related Tags
💡 Related Cases
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.