$bitNot演算子は、整数値に対してビットごとの NOT 演算を実行します。 オペランドのすべてのビットを反転し、1 を 0 に、0 を 1 に変換します。 結果は、入力値のビットごとの補数です。
構文
{
$bitNot: <expression>
}
パラメーター
| パラメーター | Description |
|---|---|
expression |
整数に評価される式。
$bitNot演算子は、この値に対してビットごとの NOT 演算を実行します。 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_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 で、ビットごとの NOT が 8 の場合は -9 になります。 観察された結果は、2 の補数表現 (~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
}
]