你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

$bitXor(按位表达式)

适用对象: MongoDB vCore

$bitXor 运算符对整数值执行按位排他 OR (XOR) 运算。 对于每个位位置,XOR作返回 1,其中作数的对应位不同,0 个位置相同。

语法

运算符的 $isArray 语法如下所示:

{
  $bitXor: [ <expression1>, <expression2>, ... ]
}

参数

DESCRIPTION
expression1, expression2, ... 解析为整数值的表达式。 该运算符按顺序对这些值执行 XOR作。

示例:

让我们了解 stores 数据集中的示例 json 的用法。

{
  "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
  "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
  "location": {
    "lat": 70.1272,
    "lon": 69.7296
  },
  "staff": {
    "totalStaff": {
      "fullTime": 19,
      "partTime": 20
    }
  },
  "sales": {
    "totalSales": 151864,
    "salesByCategory": [
      {
        "categoryName": "Sound Bars",
        "totalSales": 2120
      },
      {
        "categoryName": "Home Theater Projectors",
        "totalSales": 45004
      },
      {
        "categoryName": "Game Controllers",
        "totalSales": 43522
      },
      {
        "categoryName": "Remote Controls",
        "totalSales": 28946
      },
      {
        "categoryName": "VR Games",
        "totalSales": 32272
      }
    ]
  },
  "promotionEvents": [
    {
      "eventName": "Discount Delight Days",
      "promotionalDates": {
        "startDate": {
          "Year": 2023,
          "Month": 12,
          "Day": 26
        },
        "endDate": {
          "Year": 2024,
          "Month": 1,
          "Day": 5
        }
      },
      "discounts": [
        {
          "categoryName": "Game Controllers",
          "discountPercentage": 22
        },
        {
          "categoryName": "Home Theater Projectors",
          "discountPercentage": 23
        }
      ]
    }
  ]
}

示例 1:基本 XOR作

假设你想要在全职和兼职员工编号之间执行按位 XOR作。

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: 20,
    partTimeStaff: 19,
    staffXor: 7
  }
]

XOR 运算介于 19(二进制:10011)和 20(二进制:10100)之间产生 7(二进制:00111)。

示例 2:具有多个值的 XOR

可以通过促销事件中的多个折扣百分比执行 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: Long("16")
  }
]