$bitNot (aggregation)
On this page
Definition
New in version 6.3.
Syntax
The $bitNot
operator has the following syntax:
{ $bitNot: <expression> }
The expression can be a single argument or an array with one int
or long
element.
Behavior
Note
All numbers in mongosh
are doubles, not integers. To
specify integers in mongosh
, use the NumberInt()
or the
NumberLong()
constructor. To learn more, see Int32 or
Long.
To learn how your MongoDB driver handles numeric values, refer to your driver's documentation.
If any arguments in the array are of a different data type such as a string, double, or decimal, MongoDB returns an error.
If the expression evalutates to null
, the operation returns null
.
Example
The example on this page uses the switches
collection:
db.switches.insertMany( [ { _id: 0, a: NumberInt(0), b: NumberInt(127) }, { _id: 1, a: NumberInt(2), b: NumberInt(3) }, { _id: 2, a: NumberInt(3), b: NumberInt(5) } ] )
The following aggregation uses the $bitNot
operator in the
$project
stage:
db.switches.aggregate( [ { $project: { result: { $bitNot: "$a" } } } ])
The operation returns the following results:
[ { _id: 0, result: -1 }, { _id: 1, result: -3 }, { _id: 2, result: -4 } ]