该 $bitXor 运算符对整数值执行按位排他 OR (XOR) 运算。 对于每个位位置,XOR作返回 1,其中作数的对应位不同,以及作数所在的位置为 0。
语法
{
$bitXor: [ <expression1>, <expression2>, ... ]
}
参数
| 参数 | DESCRIPTION |
|---|---|
expression1, expression2, ... |
解析为整数值的表达式。 该运算符按顺序对这些值执行 XOR作。 |
例子
请考虑商店集合中的这个示例文档。
{
"_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:基本 XOR作
此查询使用聚合管道计算特定存储的全职员工计数和兼职人员计数。 生成的文档包含存储详细信息以及计算字段。 XOR 运算介于 19(二进制:10011)和 20(二进制:10100)之间产生 7(二进制:00111)。
db.stores.aggregate([{
$match: {
_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"
}
},
{
$project: {
name: 1,
fullTimeStaff: "$staff.totalStaff.fullTime",
partTimeStaff: "$staff.totalStaff.partTime",
staffXor: {
$bitXor: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"]
}
}
}
])
此查询返回以下结果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"fullTimeStaff": 19,
"partTimeStaff": 20,
"staffXor": 7
}
]
示例 2:具有多个值的 XOR
此查询计算特定存储事件的所有折扣百分比 Discount Delight Days 的按位 XOR。 生成的文档表示事件的所有折扣百分比的 Discount Delight Days 按位 XOR 计算。
db.stores.aggregate([{
$match: {
_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"
}
},
{
$unwind: "$promotionEvents"
},
{
$match: {
"promotionEvents.eventName": "Discount Delight Days"
}
},
{
$unwind: "$promotionEvents.discounts"
},
{
$group: {
_id: "$_id",
name: {
$first: "$name"
},
eventName: {
$first: "$promotionEvents.eventName"
},
discountPercentages: {
$push: "$promotionEvents.discounts.discountPercentage"
}
}
},
{
$project: {
name: 1,
eventName: 1,
discountPercentages: 1,
xorResult: {
$reduce: {
input: {
$map: {
input: "$discountPercentages",
as: "val",
in: {
$toLong: "$$val"
}
}
},
initialValue: {
$toLong: 0
},
in: {
$bitXor: ["$$value", "$$this"]
}
}
}
}
}
])
此查询返回以下结果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"eventName": "Discount Delight Days",
"discountPercentages": [22, 23, 10, 10, 9, 24],
"xorResult": { "$numberLong": "16" }
}
]
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。