|
| 1 | +--- |
| 2 | +title: $bitsAllClear |
| 3 | +description: The $bitsAllClear operator is used to match documents where all the bit positions specified in a bitmask are clear. |
| 4 | +type: operators |
| 5 | +category: bitwise-query |
| 6 | +--- |
| 7 | + |
| 8 | +# $bitsAllClear |
| 9 | + |
| 10 | +The `$bitsAllClear` operator is used to match documents where all the bit positions specified in a bitmask are clear (that is, 0). This operator is useful in scenarios where you need to filter documents based on specific bits being unset in a binary representation of a field. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```javascript |
| 15 | +{ |
| 16 | + <field>: { $bitsAllClear: <bitmask> } |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +## Parameters |
| 21 | + |
| 22 | +| Parameter | Description | |
| 23 | +| --- | --- | |
| 24 | +| **`field`** | The field in the document on which the bitwise operation is to be performed.| |
| 25 | +| **`<bitmask>`** | A bitmask where each bit position specifies the corresponding bit position in the field's value that must be clear (0).| |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +Consider this sample document from the stores collection. |
| 30 | + |
| 31 | +```json |
| 32 | +{ |
| 33 | + "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4", |
| 34 | + "name": "First Up Consultants | Beverage Shop - Satterfieldmouth", |
| 35 | + "location": { |
| 36 | + "lat": -89.2384, |
| 37 | + "lon": -46.4012 |
| 38 | + }, |
| 39 | + "staff": { |
| 40 | + "totalStaff": { |
| 41 | + "fullTime": 8, |
| 42 | + "partTime": 20 |
| 43 | + } |
| 44 | + }, |
| 45 | + "sales": { |
| 46 | + "totalSales": 75670, |
| 47 | + "salesByCategory": [ |
| 48 | + { |
| 49 | + "categoryName": "Wine Accessories", |
| 50 | + "totalSales": 34440 |
| 51 | + }, |
| 52 | + { |
| 53 | + "categoryName": "Bitters", |
| 54 | + "totalSales": 39496 |
| 55 | + }, |
| 56 | + { |
| 57 | + "categoryName": "Rum", |
| 58 | + "totalSales": 1734 |
| 59 | + } |
| 60 | + ] |
| 61 | + }, |
| 62 | + "promotionEvents": [ |
| 63 | + { |
| 64 | + "eventName": "Unbeatable Bargain Bash", |
| 65 | + "promotionalDates": { |
| 66 | + "startDate": { |
| 67 | + "Year": 2024, |
| 68 | + "Month": 6, |
| 69 | + "Day": 23 |
| 70 | + }, |
| 71 | + "endDate": { |
| 72 | + "Year": 2024, |
| 73 | + "Month": 7, |
| 74 | + "Day": 2 |
| 75 | + } |
| 76 | + }, |
| 77 | + "discounts": [ |
| 78 | + { |
| 79 | + "categoryName": "Whiskey", |
| 80 | + "discountPercentage": 7 |
| 81 | + }, |
| 82 | + { |
| 83 | + "categoryName": "Bitters", |
| 84 | + "discountPercentage": 15 |
| 85 | + }, |
| 86 | + { |
| 87 | + "categoryName": "Brandy", |
| 88 | + "discountPercentage": 8 |
| 89 | + }, |
| 90 | + { |
| 91 | + "categoryName": "Sports Drinks", |
| 92 | + "discountPercentage": 22 |
| 93 | + }, |
| 94 | + { |
| 95 | + "categoryName": "Vodka", |
| 96 | + "discountPercentage": 19 |
| 97 | + } |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "eventName": "Steal of a Deal Days", |
| 102 | + "promotionalDates": { |
| 103 | + "startDate": { |
| 104 | + "Year": 2024, |
| 105 | + "Month": 9, |
| 106 | + "Day": 21 |
| 107 | + }, |
| 108 | + "endDate": { |
| 109 | + "Year": 2024, |
| 110 | + "Month": 9, |
| 111 | + "Day": 29 |
| 112 | + } |
| 113 | + }, |
| 114 | + "discounts": [ |
| 115 | + { |
| 116 | + "categoryName": "Organic Wine", |
| 117 | + "discountPercentage": 19 |
| 118 | + }, |
| 119 | + { |
| 120 | + "categoryName": "White Wine", |
| 121 | + "discountPercentage": 20 |
| 122 | + }, |
| 123 | + { |
| 124 | + "categoryName": "Sparkling Wine", |
| 125 | + "discountPercentage": 19 |
| 126 | + }, |
| 127 | + { |
| 128 | + "categoryName": "Whiskey", |
| 129 | + "discountPercentage": 17 |
| 130 | + }, |
| 131 | + { |
| 132 | + "categoryName": "Vodka", |
| 133 | + "discountPercentage": 23 |
| 134 | + } |
| 135 | + ] |
| 136 | + } |
| 137 | + ] |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +The `storeFeatures` field is a bitmask integer representing various store capabilities. Each bit corresponds to a feature: |
| 142 | + |
| 143 | +| Bit | Value | Feature | |
| 144 | +|-----|-------|--------------------------| |
| 145 | +| 0 | 1 | In-Store Pickup | |
| 146 | +| 1 | 2 | Parking | |
| 147 | +| 2 | 4 | Wheelchair Access | |
| 148 | +| 3 | 8 | Open 24 Hours | |
| 149 | +| 4 | 16 | Pet-Friendly | |
| 150 | +| 5 | 32 | Free Wi-Fi | |
| 151 | +| 6 | 64 | Restrooms | |
| 152 | +| 7 | 128 | Home Delivery | |
| 153 | + |
| 154 | +### Example 1: Find stores that are not open 24 hours and do not allow pets |
| 155 | + |
| 156 | +This query retrieves stores that are NOT open 24 hours AND do NOT allow pets (bits 3 and 4) |
| 157 | + |
| 158 | +```javascript |
| 159 | +db.stores.find({ |
| 160 | + storeFeatures: { |
| 161 | + $bitsAllClear: [3, 4] |
| 162 | + } |
| 163 | +}, { |
| 164 | + _id: 1, |
| 165 | + name: 1, |
| 166 | + storeFeatures: 1 |
| 167 | +}).limit(5) |
| 168 | +``` |
| 169 | + |
| 170 | +Equivalent: |
| 171 | + |
| 172 | +```javascript |
| 173 | +db.stores.find({ |
| 174 | + storeFeatures: { |
| 175 | + $bitsAnySet: 24 |
| 176 | + } |
| 177 | + }, // 8 + 16 |
| 178 | + { |
| 179 | + _id: 1, |
| 180 | + name: 1, |
| 181 | + storeFeatures: 1 |
| 182 | + }).limit(5) |
| 183 | +``` |
| 184 | + |
| 185 | +The first five results returned by this query are: |
| 186 | + |
| 187 | +```json |
| 188 | +[ |
| 189 | + { |
| 190 | + "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6", |
| 191 | + "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort", |
| 192 | + "storeFeatures": 38 |
| 193 | + }, |
| 194 | + { |
| 195 | + "_id": "94792a4c-4b03-466b-91f6-821c4a8b2aa4", |
| 196 | + "name": "Fourth Coffee | Eyewear Shop - Lessiemouth", |
| 197 | + "storeFeatures": 225 |
| 198 | + }, |
| 199 | + { |
| 200 | + "_id": "1a2c387b-bb43-4b14-a6cd-cc05a5dbfbd5", |
| 201 | + "name": "Contoso, Ltd. | Smart Home Device Vault - Port Katarina", |
| 202 | + "storeFeatures": 36 |
| 203 | + }, |
| 204 | + { |
| 205 | + "_id": "e88f0096-4299-4944-9788-695c40786d97", |
| 206 | + "name": "Adatum Corporation | Handbag Shoppe - Lucienneberg", |
| 207 | + "storeFeatures": 135 |
| 208 | + }, |
| 209 | + { |
| 210 | + "_id": "bfb213fa-8db8-419f-8e5b-e7096120bad2", |
| 211 | + "name": "First Up Consultants | Beauty Product Shop - Hansenton", |
| 212 | + "storeFeatures": 135 |
| 213 | + } |
| 214 | +] |
| 215 | +``` |
0 commit comments