$bitsAllSet

The `$bitsAllSet` operator is used to match documents where all the specified bit positions are set (that is, are 1). This operator is useful for performing bitwise operations on fields that store integer values.

Syntax

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

Parameters

fieldstringrequired

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

<bitmask>numberrequired

A bitmask indicating which bits must be set in the field's value.

Examples

Sample Data

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

Find stores where specific feature bits are set

Matches documents where all specified bit positions in the storeFeatures field are set (1).

Query:

db.stores.find({
  storeFeatures: { $bitsAllSet: 5 }
})

Output:

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

Related