此运算符用于选择指定的任何位位置都设置为 1
的文档。 它可用于查询包含存储位掩码值的字段的文档。 当使用表示单个整数中的多个布尔标志的字段时,此运算符非常方便。
语法
{
<field>: { $bitsAnySet: [ <bit positions> ] }
}
参数
参数 | Description |
---|---|
field |
要查询的字段。 |
<bit positions> |
要检查是否将任何位置设置为 1 的位位置数组。 |
示例
请考虑商店集合中的这个示例文档。
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"location": {
"lat": -74.0427,
"lon": 160.8154
},
"staff": {
"employeeCount": {
"fullTime": 9,
"partTime": 18
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "Stockings",
"totalSales": 25731
}
],
"revenue": 25731
},
"promotionEvents": [
{
"eventName": "Mega Savings Extravaganza",
"promotionalDates": {
"startDate": { "Year": 2023, "Month": 6, "Day": 29 },
"endDate": { "Year": 2023, "Month": 7, "Day": 7 }
},
"discounts": [
{ "categoryName": "Stockings", "discountPercentage": 16 },
{ "categoryName": "Tree Ornaments", "discountPercentage": 8 }
]
},
{
"eventName": "Incredible Discount Days",
"promotionalDates": {
"startDate": { "Year": 2023, "Month": 9, "Day": 27 },
"endDate": { "Year": 2023, "Month": 10, "Day": 4 }
},
"discounts": [
{ "categoryName": "Stockings", "discountPercentage": 11 },
{ "categoryName": "Holiday Cards", "discountPercentage": 9 }
]
},
{
"eventName": "Massive Deal Mania",
"promotionalDates": {
"startDate": { "Year": 2023, "Month": 12, "Day": 26 },
"endDate": { "Year": 2024, "Month": 1, "Day": 2 }
},
"discounts": [
{ "categoryName": "Gift Bags", "discountPercentage": 21 },
{ "categoryName": "Bows", "discountPercentage": 19 }
]
},
{
"eventName": "Super Saver Soiree",
"promotionalDates": {
"startDate": { "Year": 2024, "Month": 3, "Day": 25 },
"endDate": { "Year": 2024, "Month": 4, "Day": 1 }
},
"discounts": [
{ "categoryName": "Tree Ornaments", "discountPercentage": 15 },
{ "categoryName": "Stockings", "discountPercentage": 14 }
]
},
{
"eventName": "Fantastic Savings Fiesta",
"promotionalDates": {
"startDate": { "Year": 2024, "Month": 6, "Day": 23 },
"endDate": { "Year": 2024, "Month": 6, "Day": 30 }
},
"discounts": [
{ "categoryName": "Stockings", "discountPercentage": 24 },
{ "categoryName": "Gift Wrap", "discountPercentage": 16 }
]
},
{
"eventName": "Price Plunge Party",
"promotionalDates": {
"startDate": { "Year": 2024, "Month": 9, "Day": 21 },
"endDate": { "Year": 2024, "Month": 9, "Day": 28 }
},
"discounts": [
{ "categoryName": "Holiday Tableware", "discountPercentage": 13 },
{ "categoryName": "Holiday Cards", "discountPercentage": 11 }
]
}
],
"company": "Lakeshore Retail",
"city": "Marvinfort",
"storeOpeningDate": { "$date": "2024-10-01T18:24:02.586Z" },
"lastUpdated": { "$timestamp": { "t": 1730485442, "i": 1 } },
"storeFeatures": 38
}
该 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 | 家庭送货 |
查找提供送货或免费 Wi-Fi(位 5 和 7) 的商店
示例 1:查找具有店内取件或驱动器到驱动器的商店
db.stores.find({
storeFeatures: { $bitsAnySet: [5, 7] }},
{ _id: 1, name: 1, storeFeatures: 1 }).limit(5)
等效:
db.stores.find({
storeFeatures: { $bitsAnySet: 160 }}, // 32 + 128
{ _id: 1, name: 1, storeFeatures: 1 }).limit(5)
示例输出:
[
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"storeFeatures": 38
},
{
"_id": "44fdb9b9-df83-4492-8f71-b6ef648aa312",
"name": "Fourth Coffee | Storage Solution Gallery - Port Camilla",
"storeFeatures": 222
},
{
"_id": "94792a4c-4b03-466b-91f6-821c4a8b2aa4",
"name": "Fourth Coffee | Eyewear Shop - Lessiemouth",
"storeFeatures": 225
},
{
"_id": "728c068a-638c-40af-9172-8ccfa7dddb49",
"name": "Contoso, Ltd. | Book Store - Lake Myron",
"storeFeatures": 239
},
{
"_id": "e6410bb3-843d-4fa6-8c70-7472925f6d0a",
"name": "Relecloud | Toy Collection - North Jaylan",
"storeFeatures": 108
}
]
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。