该 $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 的按位 NOT 结果为 -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
}
]
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。