翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$bitXor
バージョン 8.0.1 から新規。
Amazon DocumentDB の $bitXor演算子は、整数または長い値に対してビット単位の XOR オペレーションを実行します。
パラメータ
例 (MongoDB シェル)
次の例は、 $bitXor演算子を使用して 2 つのフィールドでビット単位の XOR を実行する方法を示しています。
サンプルドキュメントを作成する
db.flags.insertMany([
{_id: 1, a: 13, b: 10},
{_id: 2, a: 7, b: 5},
{_id: 3, a: 15, b: 9}
]);
クエリの例
db.flags.aggregate([
{ $project: { result: { $bitXor: ["$a", "$b"] } } }
]);
出力
[
{_id: 1, result: 7},
{_id: 2, result: 2},
{_id: 3, result: 6}
]
バイナリ: 13 (1101) XOR 10 (1010) = 7 (0111)、7 (0111) XOR 5 (0101) = 2 (0010)、15 (1111) XOR 9 (1001) = 6 (0110)。
コードの例
$bitXor 演算子を使用するコード例を表示するには、使用する言語のタブを選択します。
- Node.js
-
const { MongoClient } = require('mongodb');
async function example() {
const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
try {
await client.connect();
const db = client.db('test');
const collection = db.collection('flags');
const result = await collection.aggregate([
{ $project: { result: { $bitXor: ["$a", "$b"] } } }
]).toArray();
console.log(result);
} finally {
await client.close();
}
}
example();
- Python
-
from pymongo import MongoClient
def example():
client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
try:
db = client['test']
collection = db['flags']
result = list(collection.aggregate([
{'$project': {'result': {'$bitXor': ['$a', '$b']}}}
]))
print(result)
finally:
client.close()
example()