$bitXor

The `$bitXor` operator performs a bitwise exclusive OR (XOR) operation on integer values. The XOR operation returns 1 for each bit position where the corresponding bits of the operands are different, and 0 where they are the same.

Syntax

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

Parameters

expression1, expression2, ...numberrequired

Expressions that resolve to integer values. The operator performs XOR operations on these values in sequence.

Examples

Sample Data

{
  "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
  "name": "First Up Consultants | Beverage Shop",
  "staff": {
    "totalStaff": {
      "fullTime": 8,
      "partTime": 20
    }
  }
}

Basic bitwise XOR operation

Computes a bitwise XOR between the number of full-time and part-time staff.

Query:

db.stores.aggregate([
  { $match: { _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" } },
  {
    $project: {
      name: 1,
      fullTimeStaff: "$staff.totalStaff.fullTime",
      partTimeStaff: "$staff.totalStaff.partTime",
      xorResult: {
        $bitXor: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"]
      }
    }
  }
])

Output:

[
  {
    "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
    "name": "Proseware, Inc. | Home Entertainment Hub",
    "fullTimeStaff": 19,
    "partTimeStaff": 20,
    "xorResult": 7
  }
]

Related