運算子 $bitsAllSet 是用來比對設定所有指定位位置的檔(也就是 1)。 此運算子適用於在儲存整數值的欄位上執行位運算。 它可用於您需要根據整數位段中所設定的特定位來篩選文件的情況。
語法
{
<field>: { $bitsAllSet: <bitmask> }
}
參數
| 參數 | Description |
|---|---|
field |
要執行位作業之檔中的欄位。 |
<bitmask> |
位掩碼,指出欄位值中必須設定哪些位。 |
範例
請參考商店集合中的此範例檔。
{
"_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 是代表各種存放區功能的位掩碼整數。 每個位都會對應至功能:
| 位元 | 價值觀 | 特徵 / 功能 |
|---|---|---|
| 0 | 1 | In-Store 取貨 |
| 1 | 2 | 停車 |
| 2 | 4 | 輪椅存取 |
| 3 | 8 | 開啟24小時 |
| 4 | 16 | Pet-Friendly |
| 5 | 32 | 免費 Wi-Fi |
| 6 | 64 | 廁所 |
| 7 | 128 | 送貨上門 |
範例 1:尋找有停車場和衛生間的商店
此查詢會擷取 具有停車位和洗手間 的商店 (位元 1 和 6)
db.stores.find({
storeFeatures: {
$bitsAllSet: [1, 6]
}
}, {
_id: 1,
name: 1,
storeFeatures: 1
}).limit(5)
等效:
db.stores.find({
storeFeatures: {
$bitsAllSet: 66
}
}, {
_id: 1,
name: 1,
storeFeatures: 1
}).limit(5)
此查詢傳回的前五個結果如下:
[
{
"_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": "728c068a-638c-40af-9172-8ccfa7dddb49",
"name": "Contoso, Ltd. | Book Store - Lake Myron",
"storeFeatures": 239
},
{
"_id": "a2b54e5c-36cd-4a73-b547-84e21d91164e",
"name": "Contoso, Ltd. | Baby Products Corner - Port Jerrold",
"storeFeatures": 126
},
{
"_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3",
"name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview",
"storeFeatures": 107
}
]
相關內容
- 檢視從 MongoDB 遷移到 Azure DocumentDB 的選項。
- 閱讀有關 與 MongoDB 的功能相容性的更多資訊。