此运算符用于匹配位掩码中指定的任何位位置都清晰(即 0)的文档。 它可用于查询具有二进制数据或以整数存储的标志的文档。 此运算符能够根据特定位模式实现高效查询。
语法
{
<field>: { $bitsAnyClear: <bitmask> }
}
参数
| 参数 | Description |
|---|---|
field |
要查询的文档中的字段。 |
<bitmask> |
位掩码,其中每个位位置表示一个位置,用于检查它是否清晰(0)。 |
例子
请考虑商店集合中的这个示例文档。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
该 storeFeatures 字段是表示各种存储功能的位掩码整数。 每个位对应于一项功能:
| Bit | 价值 | 功能 / 特点 |
|---|---|---|
| 0 | 1 | In-Store 取件 |
| 1 | 2 | 停车 |
| 2 | 4 | 轮椅通道 |
| 3 | 8 | 打开 24 小时 |
| 4 | 16 | Pet-Friendly |
| 5 | 32 | 免费 Wi-Fi |
| 6 | 64 | 厕所 |
| 7 | 128 | 家庭送货 |
示例 1:查找没有 Wi-Fi 或店内取件的商店
此查询检索没有 Wi-Fi 或店内取件(位 0 和 5) 的商店
db.stores.find({
storeFeatures: {
$bitsAnyClear: [0, 5]
}
}, {
_id: 1,
name: 1,
storeFeatures: 1
}).limit(5)
等效:
db.stores.find({
storeFeatures: {
$bitsAnySet: 33
}
}, // 1 + 32
{
_id: 1,
name: 1,
storeFeatures: 1
}).limit(5)
此查询返回的前五个结果为:
[
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"storeFeatures": 38
},
{
"_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
"name": "Contoso, Ltd. | Office Supply Deals - South Shana",
"storeFeatures": 86
},
{
"_id": "44fdb9b9-df83-4492-8f71-b6ef648aa312",
"name": "Fourth Coffee | Storage Solution Gallery - Port Camilla",
"storeFeatures": 222
},
{
"_id": "e6410bb3-843d-4fa6-8c70-7472925f6d0a",
"name": "Relecloud | Toy Collection - North Jaylan",
"storeFeatures": 108
},
{
"_id": "9024d615-eed0-429a-a098-6640207cc2b6",
"name": "Adatum Corporation | Paper Product Mart - Lake Coralie",
"storeFeatures": 204
}
]
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。