$bitsAnyClear as bitwise query operator

APPLIES TO: MongoDB vCore

This command is used to match documents where any of the bit positions specified in a bitmask are clear (that is, 0). It's useful for querying documents with binary data or flags stored as integers. This operator enables efficient querying based on specific bit patterns.

Syntax

{
  <field>: { $bitsAnyClear: <bitmask> }
}
  • <field>: The field in the document to be queried.
  • <bitmask>: A bitmask where each bit position represents a position to check if it's clear (0).

Example

Consider a collection named stores with documents similar to the provided JSON structure. To find stores where the totalStaff.fullTime field has any of the first 3 bits clear, you can use the following query:

db.stores.find({
  "store.staff.totalStaff.fullTime": { $bitsAnyClear: 0b00000111 }
})

In this example, 0b00000111 is the bitmask representing the first 3 bits. The query would return documents where any of the first 3 bits of the totalStaff.fullTime field are clear.