- Reference >
- Operators >
- Update Operators >
- Bitwise Update Operator >
- $bit
$bit¶
On this page
Definition¶
-
$bit¶ The
$bitoperator performs a bitwise update of a field. The operator supports bitwiseand, bitwiseor, and bitwisexor(i.e. exclusive or) operations. To specify a$bitoperator expression, use the following prototype:Only use this operator with integer fields (either 32-bit integer or 64-bit integer).
To specify a
<field>in an embedded document or in an array, use dot notation.Note
All numbers in the
mongoshell are doubles, not integers. Use theNumberInt()or theNumberLong()constructor to specify integers. See NumberInt or NumberLong for more information.
Examples¶
Bitwise AND¶
Consider the following document inserted into the collection
switches:
The following update() operation updates the
expdata field to the result of a bitwise and operation between
the current value NumberInt(13) (i.e. 1101) and
NumberInt(10) (i.e. 1010):
The bitwise and operation results in the integer 8 (i.e. 1000):
And the updated document has the following value for expdata:
The mongo shell displays NumberInt(8) as 8.
Bitwise OR¶
Consider the following document inserted into the collection
switches:
The following update() operation updates the
expdata field to the result of a bitwise or operation between
the current value NumberLong(3) (i.e. 0011) and
NumberInt(5) (i.e. 0101):
The bitwise or operation results in the integer 7 (i.e. 0111):
And the updated document has the following value for expdata:
Bitwise XOR¶
Consider the following document in the collection switches:
The following update() operation updates the
expdata field to the result of a bitwise xor operation between
the current value NumberLong(1) (i.e. 0001) and
NumberInt(5) (i.e. 0101):
The bitwise xor operation results in the integer 4:
And the updated document has the following value for expdata: