$bitsAllClear

The `$bitsAllClear` operator is used to match documents where all the bit positions specified in a bitmask are clear (that is, 0). This operator is useful in scenarios where you need to filter documents based on specific bits being unset in a binary representation of a field.

Syntax

{
  <field>: { $bitsAllClear: <bitmask> }
}

Parameters

fieldstringrequired

The field in the document on which the bitwise operation is to be performed.

<bitmask>numberrequired

A bitmask where each bit position specifies the corresponding bit position in the field's value that must be clear (0).

Examples

Sample Data

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

Find stores where specific feature bits are clear

Matches documents where all specified bit positions in the storeFeatures field are clear (0).

Query:

db.stores.find({
  storeFeatures: { $bitsAllClear: 2 }
})

Output:

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

Related