适用对象: MongoDB vCore
此命令用于选择其中任何指定的位位置设置为 1
的文档。 它可用于查询包含存储位掩码值的字段的文档。 当使用表示单个整数中的多个布尔标志的字段时,此运算符非常方便。
语法
{
<field>: { $bitsAnySet: [ <bit positions> ] }
}
<field>
:要查询的字段。<bit positions>
:位位置数组,检查是否有任何位位置设置为1
。
示例
示例 1:查询设置了特定位位置的文档
假设我们有一个名为 stores
的集合,我们希望查找 storeId
字段中的任何位位置 1 或 3 设置为 1
的所有存储。
db.stores.find({
"store.storeId": { $bitsAnySet: [1, 3] }
})
示例 2:查询嵌套字段中具有位位置的文档
假设我们想要查找“笔记本电脑”类别的 discountPercentage
中的任意位位置 0 或 2 设置为 1
的所有促销活动。
db.stores.find({
"store.promotionEvents.discounts": {
$elemMatch: {
"categoryName": "Laptops",
"discountPercentage": { $bitsAnySet: [0, 2] }
}
}
})