$bit

The `$bit` operator performs bitwise updates on integer fields. It supports bitwise AND, OR, and XOR operations.

Syntax

{
  $bit: {
    <field>: { <and | or | xor>: <integer> }
  }
}

Parameters

fieldstringrequired

The field to update with bitwise operations.

and | or | xorstringrequired

The bitwise operation to perform (and, or, or xor).

<integer>numberrequired

The integer value to use in the bitwise operation.

Examples

Sample Data

{
  "_id": "store1",
  "name": "Store Name",
  "storeFeatures": 5
}

Perform bitwise AND operation

Updates the storeFeatures field using a bitwise AND operation.

Query:

db.stores.update(
  { _id: "store1" },
  { $bit: { storeFeatures: { and: 3 } } }
)

Output:

{
  "acknowledged": true,
  "matchedCount": 1,
  "modifiedCount": 1
}

Related