$bitAnd 연산자는 정수 값에 대한 작업을 수행합니다bitwise AND. 첫 번째 피연산자의 각 비트를 두 번째 피연산자의 해당 비트와 비교합니다. 두 비트가 모두 1이면 해당 결과 비트가 1로 설정됩니다. 그렇지 않으면 해당 결과 비트가 0으로 설정됩니다.
문법
{
$bitAnd: [ <expression1>, <expression2>, ... ]
}
매개 변수
| 매개 변수 | Description |
|---|---|
expression1, expression2, ... |
정수로 계산되는 식입니다. 연산자는 $bitAnd 제공된 모든 식에 대해 비트 AND 연산을 수행합니다. |
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
예제 1: 기본 비트 AND 연산
이 쿼리는 특정 저장소에 대한 직원 정보를 검색하고 권한 플래그를 bitwise AND 만들기 위해 정규직과 파트타임 직원 수를 계산합니다.
db.stores.aggregate([{
$match: {
_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"
}
},
{
$project: {
name: 1,
fullTimeStaff: "$staff.totalStaff.fullTime",
partTimeStaff: "$staff.totalStaff.partTime",
staffPermissionFlag: {
$bitAnd: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"]
}
}
}
])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"fullTimeStaff": 19,
"partTimeStaff": 20,
"staffPermissionFlag": 16
}
]
예제 2: 여러 값 $bitAnd
이 쿼리는 단일 저장소에 대한 여러 숫자 필드를 기반으로 비트 사용 권한 또는 결합된 플래그를 확인합니다.
db.stores.aggregate([{
$match: {
_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"
}
},
{
$project: {
name: 1,
combinedFlag: {
$bitAnd: [
"$staff.totalStaff.fullTime",
"$staff.totalStaff.partTime",
255
]
}
}
}
])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"combinedFlag": 16
}
]