기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
$bitXor
버전 8.0.1에서 새로 추가되었습니다.
Amazon DocumentDB의 $bitXor 연산자는 정수 또는 긴 값에 대해 비트 단위 XOR 작업을 수행합니다.
파라미터
예제(MongoDB 쉘)
다음 예제에서는 $bitXor 연산자를 사용하여 두 필드에서 비트 단위 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()