$bitsAllClear運算子用來比對位掩碼中指定的所有位位置都清楚的檔(也就是 0)。 當您需要根據欄位的二進位表示法中未設定的特定位來篩選檔時,此運算子很有用。
語法
{
<field>: { $bitsAllClear: <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 是代表各種存放區功能的位掩碼整數。 每個位都會對應至功能:
| 位元 | 價值觀 | 特徵 / 功能 |
|---|---|---|
| 0 | 1 | In-Store 取貨 |
| 1 | 2 | 停車 |
| 2 | 4 | 輪椅存取 |
| 3 | 8 | 開啟24小時 |
| 4 | 16 | Pet-Friendly |
| 5 | 32 | 免費 Wi-Fi |
| 6 | 64 | 廁所 |
| 7 | 128 | 送貨上門 |
範例 1:尋找 24 小時不營業且不允許攜帶寵物的商店
此查詢會擷取未 24 小時營業且不允許攜帶寵物的商店 (位元 3 和 4)
db.stores.find({
storeFeatures: {
$bitsAllClear: [3, 4]
}
}, {
_id: 1,
name: 1,
storeFeatures: 1
}).limit(5)
等效:
db.stores.find({
storeFeatures: {
$bitsAnySet: 24
}
}, // 8 + 16
{
_id: 1,
name: 1,
storeFeatures: 1
}).limit(5)
此查詢傳回的前五個結果如下:
[
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"storeFeatures": 38
},
{
"_id": "94792a4c-4b03-466b-91f6-821c4a8b2aa4",
"name": "Fourth Coffee | Eyewear Shop - Lessiemouth",
"storeFeatures": 225
},
{
"_id": "1a2c387b-bb43-4b14-a6cd-cc05a5dbfbd5",
"name": "Contoso, Ltd. | Smart Home Device Vault - Port Katarina",
"storeFeatures": 36
},
{
"_id": "e88f0096-4299-4944-9788-695c40786d97",
"name": "Adatum Corporation | Handbag Shoppe - Lucienneberg",
"storeFeatures": 135
},
{
"_id": "bfb213fa-8db8-419f-8e5b-e7096120bad2",
"name": "First Up Consultants | Beauty Product Shop - Hansenton",
"storeFeatures": 135
}
]
相關內容
- 檢視從 MongoDB 遷移到 Azure DocumentDB 的選項。
- 閱讀有關 與 MongoDB 的功能相容性的更多資訊。