연산자는 $bitNot 정수 값에 대해 비트 NOT 연산을 수행합니다. 피연산자의 모든 비트를 반전하여 1을 0과 0으로 1로 바꿔줍니다. 결과는 입력 값의 비트 보수입니다.
문법
{
$bitNot: <expression>
}
매개 변수
| 매개 변수 | Description |
|---|---|
expression |
정수로 계산되는 식입니다. 연산자는 이 $bitNot 값에 대해 비트 NOT 연산을 수행합니다. |
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_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: 기본 비트 NOT 연산
이 쿼리는 특정 저장소 문서에 대한 직원 수 필드에 대해 비트 반전을 수행합니다. 반전된 값은 특수 권한 플래그, 기능 토글 또는 비트 마스크 작업에 사용할 수 있습니다. 14개 결과의 비트 NOT은 -15이고, 비트는 8이 아니라 -9입니다. 관찰된 결과는 ~n = -(n+1)인 두 개의 보수 표현 때문입니다.
db.stores.aggregate([{
$match: {
_id: "26afb024-53c7-4e94-988c-5eede72277d5"
}
},
{
$project: {
name: 1,
fullTimeStaff: "$staff.totalStaff.fullTime",
partTimeStaff: "$staff.totalStaff.partTime",
invertedFullTime: {
$bitNot: "$staff.totalStaff.fullTime"
},
invertedPartTime: {
$bitNot: "$staff.totalStaff.partTime"
}
}
}
])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
"fullTimeStaff": 14,
"partTimeStaff": 8,
"invertedFullTime": -15,
"invertedPartTime": -9
}
]
예제 2: 할인율과 함께 $bitNot 사용
이 쿼리는 특정 저장소에 대한 할인 정보를 추출하고 처리하며 각 할인 비율에 대해 비트 NOT 연산을 적용합니다. 비트 NOT 연산은 모든 비트를 반전합니다. 20은 -21 되고 17은 -18이 됩니다.
db.stores.aggregate([{
$match: {
_id: "26afb024-53c7-4e94-988c-5eede72277d5"
}
},
{
$unwind: "$promotionEvents"
},
{
$match: {
"promotionEvents.eventName": "Incredible Savings Showcase"
}
},
{
$unwind: "$promotionEvents.discounts"
},
{
$project: {
name: 1,
eventName: "$promotionEvents.eventName",
categoryName: "$promotionEvents.discounts.categoryName",
discountPercentage: "$promotionEvents.discounts.discountPercentage",
invertedDiscount: {
$bitNot: "$promotionEvents.discounts.discountPercentage"
}
}
}
])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
"eventName": "Incredible Savings Showcase",
"categoryName": "Microphone Stands",
"discountPercentage": 17,
"invertedDiscount": -18
},
{
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
"eventName": "Incredible Savings Showcase",
"categoryName": "Condenser Microphones",
"discountPercentage": 20,
"invertedDiscount": -21
}
]