Skip to content

Commit 4fe84ec

Browse files
authored
Update $setField operator documentation
1 parent ed722db commit 4fe84ec

File tree

1 file changed

+115
-3
lines changed
  • articles/cosmos-db/mongodb/vcore/operators/object-expression

1 file changed

+115
-3
lines changed

articles/cosmos-db/mongodb/vcore/operators/object-expression/$setField.md

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
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)
44
description: The setField command is used to add, update, or remove fields in embedded documents.
55
author: avijitgupta
66
ms.author: avijitgupta
77
ms.service: azure-cosmos-db
88
ms.subservice: mongodb-vcore
99
ms.topic: language-reference
10-
ms.date: 08/03/2025
10+
ms.date: 09/04/2025
1111
---
1212

1313
# $setField
@@ -36,6 +36,118 @@ The `$setField` operator is used to add, update, or remove fields in embedded do
3636

3737
## Examples
3838

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+
39151
### Example 1: Updating a nested field
40152

41153
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(
82194

83195
### Example 2: Removing a field
84196

85-
Suppose you want to remove the `totalStaff` field from the `staff` object.
197+
This query removes the `totalStaff` field from the `staff` object.
86198

87199
```javascript
88200
db.collection.updateOne(

0 commit comments

Comments
 (0)