|
| 1 | +--- |
| 2 | +title: $ |
| 3 | +description: The $ positional operator identifies an element in an array to update without explicitly specifying the position of the element in the array. |
| 4 | +type: operators |
| 5 | +category: array-update |
| 6 | +--- |
| 7 | + |
| 8 | +# $ |
| 9 | + |
| 10 | +The `$` positional operator identifies an element in an array to update without explicitly specifying the position of the element in the array. The `$` operator acts as a placeholder for the first element that matches the query condition, and the array field must appear as part of the query document. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```javascript |
| 15 | +db.collection.updateOne( |
| 16 | + { <array>: <value> }, |
| 17 | + { <update operator>: { "<array>.$": <value> } } |
| 18 | +) |
| 19 | +``` |
| 20 | + |
| 21 | +## Parameters |
| 22 | + |
| 23 | +| Parameter | Description | |
| 24 | +| --- | --- | |
| 25 | +| **`array`** | The array field that contains the element to update. Must be part of the query condition. | |
| 26 | +| **`value`** | The value used to match the array element in the query condition. | |
| 27 | +| **`update operator`** | The update operator to apply (for example, `$set`, `$inc`, `$unset`). | |
| 28 | + |
| 29 | +## Examples |
| 30 | + |
| 31 | +Consider this sample document from the stores collection. |
| 32 | + |
| 33 | +```json |
| 34 | +{ |
| 35 | + "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4", |
| 36 | + "name": "First Up Consultants | Beverage Shop - Satterfieldmouth", |
| 37 | + "location": { |
| 38 | + "lat": -89.2384, |
| 39 | + "lon": -46.4012 |
| 40 | + }, |
| 41 | + "staff": { |
| 42 | + "totalStaff": { |
| 43 | + "fullTime": 8, |
| 44 | + "partTime": 20 |
| 45 | + } |
| 46 | + }, |
| 47 | + "sales": { |
| 48 | + "totalSales": 75670, |
| 49 | + "salesByCategory": [ |
| 50 | + { |
| 51 | + "categoryName": "Wine Accessories", |
| 52 | + "totalSales": 34440 |
| 53 | + }, |
| 54 | + { |
| 55 | + "categoryName": "Bitters", |
| 56 | + "totalSales": 39496 |
| 57 | + }, |
| 58 | + { |
| 59 | + "categoryName": "Rum", |
| 60 | + "totalSales": 1734 |
| 61 | + } |
| 62 | + ] |
| 63 | + }, |
| 64 | + "promotionEvents": [ |
| 65 | + { |
| 66 | + "eventName": "Unbeatable Bargain Bash", |
| 67 | + "promotionalDates": { |
| 68 | + "startDate": { |
| 69 | + "Year": 2024, |
| 70 | + "Month": 6, |
| 71 | + "Day": 23 |
| 72 | + }, |
| 73 | + "endDate": { |
| 74 | + "Year": 2024, |
| 75 | + "Month": 7, |
| 76 | + "Day": 2 |
| 77 | + } |
| 78 | + }, |
| 79 | + "discounts": [ |
| 80 | + { |
| 81 | + "categoryName": "Whiskey", |
| 82 | + "discountPercentage": 7 |
| 83 | + }, |
| 84 | + { |
| 85 | + "categoryName": "Bitters", |
| 86 | + "discountPercentage": 15 |
| 87 | + }, |
| 88 | + { |
| 89 | + "categoryName": "Brandy", |
| 90 | + "discountPercentage": 8 |
| 91 | + }, |
| 92 | + { |
| 93 | + "categoryName": "Sports Drinks", |
| 94 | + "discountPercentage": 22 |
| 95 | + }, |
| 96 | + { |
| 97 | + "categoryName": "Vodka", |
| 98 | + "discountPercentage": 19 |
| 99 | + } |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "eventName": "Steal of a Deal Days", |
| 104 | + "promotionalDates": { |
| 105 | + "startDate": { |
| 106 | + "Year": 2024, |
| 107 | + "Month": 9, |
| 108 | + "Day": 21 |
| 109 | + }, |
| 110 | + "endDate": { |
| 111 | + "Year": 2024, |
| 112 | + "Month": 9, |
| 113 | + "Day": 29 |
| 114 | + } |
| 115 | + }, |
| 116 | + "discounts": [ |
| 117 | + { |
| 118 | + "categoryName": "Organic Wine", |
| 119 | + "discountPercentage": 19 |
| 120 | + }, |
| 121 | + { |
| 122 | + "categoryName": "White Wine", |
| 123 | + "discountPercentage": 20 |
| 124 | + }, |
| 125 | + { |
| 126 | + "categoryName": "Sparkling Wine", |
| 127 | + "discountPercentage": 19 |
| 128 | + }, |
| 129 | + { |
| 130 | + "categoryName": "Whiskey", |
| 131 | + "discountPercentage": 17 |
| 132 | + }, |
| 133 | + { |
| 134 | + "categoryName": "Vodka", |
| 135 | + "discountPercentage": 23 |
| 136 | + } |
| 137 | + ] |
| 138 | + } |
| 139 | + ] |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +### Example 1: Project the first element of an array, matching the condition |
| 144 | + |
| 145 | +This query returns the first element within the `salesByCategory` array, for `DJ` equipment with `totalSales` greater than 35000. |
| 146 | + |
| 147 | +```javascript |
| 148 | +db.stores.find({ |
| 149 | + "sales.salesByCategory": { |
| 150 | + $elemMatch: { |
| 151 | + categoryName: { |
| 152 | + $regex: "^DJ" |
| 153 | + } |
| 154 | + } |
| 155 | + }, |
| 156 | + "sales.salesByCategory.totalSales": { |
| 157 | + $gt: 35000 |
| 158 | + } |
| 159 | +}, { |
| 160 | + "sales.salesByCategory.$": 1 |
| 161 | +}).limit(2) |
| 162 | +``` |
| 163 | + |
| 164 | +The first two results returned by this query are: |
| 165 | + |
| 166 | +```json |
| 167 | +[ |
| 168 | + { |
| 169 | + "_id": "d3c9df51-41bd-4b4e-a26b-b038d9cf8b45", |
| 170 | + "sales": { |
| 171 | + "salesByCategory": [ |
| 172 | + { |
| 173 | + "categoryName": "DJ Speakers", |
| 174 | + "totalSales": 36972 |
| 175 | + } |
| 176 | + ] |
| 177 | + } |
| 178 | + }, |
| 179 | + { |
| 180 | + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", |
| 181 | + "sales": { |
| 182 | + "salesByCategory": [ |
| 183 | + { |
| 184 | + "categoryName": "DJ Headphones", |
| 185 | + "totalSales": 35911 |
| 186 | + } |
| 187 | + ] |
| 188 | + } |
| 189 | + } |
| 190 | +] |
| 191 | +``` |
| 192 | + |
| 193 | +### Example 2: Update discount percentage for a specific category |
| 194 | + |
| 195 | +This query updates the discount percentage for "Desks" category in the first matching promotion event. |
| 196 | + |
| 197 | +```javascript |
| 198 | +db.stores.updateOne( |
| 199 | + { |
| 200 | + _id: "905d1939-e03a-413e-a9c4-221f74055aac", |
| 201 | + "promotionEvents.discounts.categoryName": "Desks" |
| 202 | + }, |
| 203 | + { |
| 204 | + $set: { "promotionEvents.$.discounts.$[elem].discountPercentage": 25 } |
| 205 | + }, |
| 206 | + { |
| 207 | + arrayFilters: [{ "elem.categoryName": "Desks" }] |
| 208 | + } |
| 209 | +) |
| 210 | +``` |
| 211 | + |
| 212 | +### Example 3: Update sales category total |
| 213 | + |
| 214 | +This query updates the total sales for a specific category using the `$ (positional operator)`. |
| 215 | + |
| 216 | +```javascript |
| 217 | +db.stores.updateOne( |
| 218 | + { |
| 219 | + _id: "905d1939-e03a-413e-a9c4-221f74055aac", |
| 220 | + "sales.salesByCategory.categoryName": "Desk Lamps" |
| 221 | + }, |
| 222 | + { |
| 223 | + $inc: { "sales.salesByCategory.$.totalSales": 1000 } |
| 224 | + } |
| 225 | +) |
| 226 | +``` |
0 commit comments