$bit - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$bit

Amazon DocumentDB の $bit演算子を使用すると、特定のフィールドのビットに対してビット単位のオペレーションを実行できます。これは、数値内の個々のビットの状態の設定、クリア、チェックなどのタスクに役立ちます。

パラメータ

  • field: ビット単位のオペレーションを実行するフィールド。

  • and: フィールドでビット単位の AND オペレーションを実行するために使用される整数値。

  • or: フィールドでビット単位の OR オペレーションを実行するために使用される整数値。

  • xor: フィールドでビット単位の XOR オペレーションを実行するために使用される整数値。

例 (MongoDB シェル)

次の例は、 $bit演算子を使用して数値フィールドに対してビット単位のオペレーションを実行する方法を示しています。

サンプルドキュメントを作成する

db.numbers.insert([ { "_id": 1, "number": 5 }, { "_id": 2, "number": 12 } ])

クエリの例

db.numbers.update( { "_id": 1 }, { "$bit": { "number": { "and": 3 } } } )

出力

{ "_id": 1, "number": 1 }

この例では、 $bit演算子を使用して、 が 1 のドキュメントの「数値」フィールドに対してビット単位_idの AND オペレーションを実行します。その結果、「数値」フィールドの値は 1 に設定されます。これは、元の値 5 と値 3 の間のビット単位の AND 演算の結果です。

コードの例

$bit コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。

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()