기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
$bitAnd
버전 8.0.1에서 새로 추가되었습니다.
Amazon DocumentDB의 $bitAnd 연산자는 정수 또는 긴 값에 대해 비트 단위 AND 작업을 수행합니다.
파라미터
예제(MongoDB 쉘)
다음 예제에서는 $bitAnd 연산자를 사용하여 두 필드에 대해 비트 단위 AND를 수행하는 방법을 보여줍니다.
샘플 문서 생성
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: { $bitAnd: ["$a", "$b"] } } }
]);
출력
[
{_id: 1, result: 8},
{_id: 2, result: 5},
{_id: 3, result: 9}
]
바이너리: 13(1101) AND 10(1010) = 8(1000), 7(0111) AND 5(0101) = 5(0101), 15(1111) AND 9(1001) = 9(1001).
코드 예제
$bitAnd 연산자 사용에 대한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.
- 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: { $bitAnd: ["$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': {'$bitAnd': ['$a', '$b']}}}
]))
print(result)
finally:
client.close()
example()