$bitOr

The `$bitOr` operator performs a bitwise OR operation on integer values. It compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1. If both bits are 0, the corresponding result bit is set to 0.

Syntax

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

Parameters

expression1, expression2, ...numberrequired

Expressions that evaluate to integers. The $bitOr operator performs a bitwise OR operation on all provided expressions.

Examples

Sample Data

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

Basic bitwise OR operation

Computes a bitwise OR 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",
      combinedFlag: {
        $bitOr: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"]
      }
    }
  }
])

Output:

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

Related