$bit
The $bit operator in Amazon DocumentDB allows you to perform bitwise operations on the bits of a given field. This can be useful for tasks like setting, clearing, or checking the state of individual bits within a number.
Parameters
-
field: The field to perform bitwise operations on. -
and: An integer value that is used to perform a bitwise AND operation on the field. -
or: An integer value that is used to perform a bitwise OR operation on the field. -
xor: An integer value that is used to perform a bitwise XOR operation on the field.
Example (MongoDB Shell)
The following example demonstrates how to use the $bit operator to perform bitwise operations on a numerical field.
Create sample documents
db.numbers.insert([ { "_id": 1, "number": 5 }, { "_id": 2, "number": 12 } ])
Query example
db.numbers.update( { "_id": 1 }, { "$bit": { "number": { "and": 3 } } } )
Output
{
"_id": 1,
"number": 1
}
In this example, the $bit operator is used to perform a bitwise AND operation on the "number" field of the document with the _id of 1. The result is that the value of the "number" field is set to 1, which is the result of the bitwise AND operation between the original value of 5 and the value 3.
Code examples
To view a code example for using the $bit command, choose the tab for the language that you want to use: