Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
APPLIES TO:
MongoDB vCore
This command is used to select documents where any of the bit positions specified are set to 1
. It's useful for querying documents with fields that store bitmask values. This operator can be handy when working with fields that represent multiple boolean flags in a single integer.
Syntax
{
<field>: { $bitsAnySet: [ <bit positions> ] }
}
<field>
: The field to be queried.<bit positions>
: An array of bit positions to check if any are set to1
.
Examples
Example 1: Querying for Documents with Specific Bit Positions Set
Suppose we have a collection named stores
and we want to find all stores where any of the bit positions 1 or 3 in the storeId
field are set to 1
.
db.stores.find({
"store.storeId": { $bitsAnySet: [1, 3] }
})
Example 2: Querying for Documents with Bit Positions in Nested Fields
Suppose we want to find all promotion events where any of the bit positions 0 or 2 in the discountPercentage
for the "Laptops" category are set to 1
.
db.stores.find({
"store.promotionEvents.discounts": {
$elemMatch: {
"categoryName": "Laptops",
"discountPercentage": { $bitsAnySet: [0, 2] }
}
}
})
Related content
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB