$bit - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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:

Node.js
const { MongoClient } = require('mongodb'); async function main() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('numbers'); await collection.updateOne( { "_id": 1 }, { "$bit": { "number": { "and": 3 } } } ); const result = await collection.findOne({ "_id": 1 }); console.log(result); await client.close(); } main();
Python
from pymongo import MongoClient client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['numbers'] collection.update_one( {"_id": 1}, {"$bit": {"number": {"and": 3}}} ) result = collection.find_one({"_id": 1}) print(result) client.close()