|
1 | 1 | --- |
2 | 2 | title: $setField |
3 | | - titleSuffix: Overview of the $setField expression in Azure Cosmos DB for MongoDB (vCore) |
| 3 | + titleSuffix: Overview of the $setField operator in Azure Cosmos DB for MongoDB (vCore) |
4 | 4 | description: The setField command is used to add, update, or remove fields in embedded documents. |
5 | 5 | author: avijitgupta |
6 | 6 | ms.author: avijitgupta |
7 | 7 | ms.service: azure-cosmos-db |
8 | 8 | ms.subservice: mongodb-vcore |
9 | 9 | ms.topic: language-reference |
10 | | - ms.date: 08/03/2025 |
| 10 | + ms.date: 09/04/2025 |
11 | 11 | --- |
12 | 12 |
|
13 | 13 | # $setField |
@@ -36,6 +36,118 @@ The `$setField` operator is used to add, update, or remove fields in embedded do |
36 | 36 |
|
37 | 37 | ## Examples |
38 | 38 |
|
| 39 | +Consider this sample document from the stores collection. |
| 40 | + |
| 41 | +```json |
| 42 | +{ |
| 43 | + "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4", |
| 44 | + "name": "First Up Consultants | Beverage Shop - Satterfieldmouth", |
| 45 | + "location": { |
| 46 | + "lat": -89.2384, |
| 47 | + "lon": -46.4012 |
| 48 | + }, |
| 49 | + "staff": { |
| 50 | + "totalStaff": { |
| 51 | + "fullTime": 8, |
| 52 | + "partTime": 20 |
| 53 | + } |
| 54 | + }, |
| 55 | + "sales": { |
| 56 | + "totalSales": 75670, |
| 57 | + "salesByCategory": [ |
| 58 | + { |
| 59 | + "categoryName": "Wine Accessories", |
| 60 | + "totalSales": 34440 |
| 61 | + }, |
| 62 | + { |
| 63 | + "categoryName": "Bitters", |
| 64 | + "totalSales": 39496 |
| 65 | + }, |
| 66 | + { |
| 67 | + "categoryName": "Rum", |
| 68 | + "totalSales": 1734 |
| 69 | + } |
| 70 | + ] |
| 71 | + }, |
| 72 | + "promotionEvents": [ |
| 73 | + { |
| 74 | + "eventName": "Unbeatable Bargain Bash", |
| 75 | + "promotionalDates": { |
| 76 | + "startDate": { |
| 77 | + "Year": 2024, |
| 78 | + "Month": 6, |
| 79 | + "Day": 23 |
| 80 | + }, |
| 81 | + "endDate": { |
| 82 | + "Year": 2024, |
| 83 | + "Month": 7, |
| 84 | + "Day": 2 |
| 85 | + } |
| 86 | + }, |
| 87 | + "discounts": [ |
| 88 | + { |
| 89 | + "categoryName": "Whiskey", |
| 90 | + "discountPercentage": 7 |
| 91 | + }, |
| 92 | + { |
| 93 | + "categoryName": "Bitters", |
| 94 | + "discountPercentage": 15 |
| 95 | + }, |
| 96 | + { |
| 97 | + "categoryName": "Brandy", |
| 98 | + "discountPercentage": 8 |
| 99 | + }, |
| 100 | + { |
| 101 | + "categoryName": "Sports Drinks", |
| 102 | + "discountPercentage": 22 |
| 103 | + }, |
| 104 | + { |
| 105 | + "categoryName": "Vodka", |
| 106 | + "discountPercentage": 19 |
| 107 | + } |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "eventName": "Steal of a Deal Days", |
| 112 | + "promotionalDates": { |
| 113 | + "startDate": { |
| 114 | + "Year": 2024, |
| 115 | + "Month": 9, |
| 116 | + "Day": 21 |
| 117 | + }, |
| 118 | + "endDate": { |
| 119 | + "Year": 2024, |
| 120 | + "Month": 9, |
| 121 | + "Day": 29 |
| 122 | + } |
| 123 | + }, |
| 124 | + "discounts": [ |
| 125 | + { |
| 126 | + "categoryName": "Organic Wine", |
| 127 | + "discountPercentage": 19 |
| 128 | + }, |
| 129 | + { |
| 130 | + "categoryName": "White Wine", |
| 131 | + "discountPercentage": 20 |
| 132 | + }, |
| 133 | + { |
| 134 | + "categoryName": "Sparkling Wine", |
| 135 | + "discountPercentage": 19 |
| 136 | + }, |
| 137 | + { |
| 138 | + "categoryName": "Whiskey", |
| 139 | + "discountPercentage": 17 |
| 140 | + }, |
| 141 | + { |
| 142 | + "categoryName": "Vodka", |
| 143 | + "discountPercentage": 23 |
| 144 | + } |
| 145 | + ] |
| 146 | + } |
| 147 | + ] |
| 148 | +} |
| 149 | +``` |
| 150 | + |
39 | 151 | ### Example 1: Updating a nested field |
40 | 152 |
|
41 | 153 | This query performs a conditional update on nested discount values inside promotion events for the document matching a specific `_id`. |
@@ -82,7 +194,7 @@ db.stores.updateOne( |
82 | 194 |
|
83 | 195 | ### Example 2: Removing a field |
84 | 196 |
|
85 | | -Suppose you want to remove the `totalStaff` field from the `staff` object. |
| 197 | +This query removes the `totalStaff` field from the `staff` object. |
86 | 198 |
|
87 | 199 | ```javascript |
88 | 200 | db.collection.updateOne( |
|
0 commit comments