Skip to content

Commit 71836ed

Browse files
committed
Add reference files
1 parent 3a2123c commit 71836ed

34 files changed

+7462
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
---
2+
title: $addFields
3+
description: The $addFields stage in the aggregation pipeline is used to add new fields to documents.
4+
type: operators
5+
category: aggregation
6+
---
7+
8+
# $addFields
9+
10+
The $addFields stage in the aggregation pipeline is used to add new fields to documents. It can also be used to reset the values of existing fields. This stage is particularly useful when you need to create new fields based on existing data or modify existing fields within your documents.
11+
12+
## Syntax
13+
14+
```javascript
15+
{
16+
$addFields: {
17+
<newField1>: <expression1>,
18+
<newField2>: <expression2>,
19+
...
20+
}
21+
}
22+
```
23+
24+
## Parameters
25+
26+
| Parameter | Description |
27+
| --- | --- |
28+
| **`newField1`** | The name of the new field to add or the existing field to modify. |
29+
| **`expression1`** | The expression to compute the value of newField1. |
30+
31+
## Examples
32+
33+
Consider this sample document from the stores collection.
34+
35+
```json
36+
{
37+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
38+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
39+
"location": {
40+
"lat": -89.2384,
41+
"lon": -46.4012
42+
},
43+
"staff": {
44+
"totalStaff": {
45+
"fullTime": 8,
46+
"partTime": 20
47+
}
48+
},
49+
"sales": {
50+
"totalSales": 75670,
51+
"salesByCategory": [
52+
{
53+
"categoryName": "Wine Accessories",
54+
"totalSales": 34440
55+
},
56+
{
57+
"categoryName": "Bitters",
58+
"totalSales": 39496
59+
},
60+
{
61+
"categoryName": "Rum",
62+
"totalSales": 1734
63+
}
64+
]
65+
},
66+
"promotionEvents": [
67+
{
68+
"eventName": "Unbeatable Bargain Bash",
69+
"promotionalDates": {
70+
"startDate": {
71+
"Year": 2024,
72+
"Month": 6,
73+
"Day": 23
74+
},
75+
"endDate": {
76+
"Year": 2024,
77+
"Month": 7,
78+
"Day": 2
79+
}
80+
},
81+
"discounts": [
82+
{
83+
"categoryName": "Whiskey",
84+
"discountPercentage": 7
85+
},
86+
{
87+
"categoryName": "Bitters",
88+
"discountPercentage": 15
89+
},
90+
{
91+
"categoryName": "Brandy",
92+
"discountPercentage": 8
93+
},
94+
{
95+
"categoryName": "Sports Drinks",
96+
"discountPercentage": 22
97+
},
98+
{
99+
"categoryName": "Vodka",
100+
"discountPercentage": 19
101+
}
102+
]
103+
},
104+
{
105+
"eventName": "Steal of a Deal Days",
106+
"promotionalDates": {
107+
"startDate": {
108+
"Year": 2024,
109+
"Month": 9,
110+
"Day": 21
111+
},
112+
"endDate": {
113+
"Year": 2024,
114+
"Month": 9,
115+
"Day": 29
116+
}
117+
},
118+
"discounts": [
119+
{
120+
"categoryName": "Organic Wine",
121+
"discountPercentage": 19
122+
},
123+
{
124+
"categoryName": "White Wine",
125+
"discountPercentage": 20
126+
},
127+
{
128+
"categoryName": "Sparkling Wine",
129+
"discountPercentage": 19
130+
},
131+
{
132+
"categoryName": "Whiskey",
133+
"discountPercentage": 17
134+
},
135+
{
136+
"categoryName": "Vodka",
137+
"discountPercentage": 23
138+
}
139+
]
140+
}
141+
]
142+
}
143+
```
144+
145+
### Example 1: Adding a new field
146+
147+
This query adds a new field totalDiscountEvents that counts the number of promotion events
148+
149+
```javascript
150+
db.stores.aggregate([
151+
{
152+
$addFields: {
153+
totalDiscountEvents: { $size: "$store.promotionEvents" }
154+
}
155+
}
156+
])
157+
```
158+
159+
This query returns the following result:
160+
161+
```json
162+
[
163+
{
164+
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
165+
"store": {
166+
"name": "Downtown Store",
167+
"promotionEvents": ["Summer Sale", "Black Friday", "Holiday Deals"]
168+
},
169+
"totalDiscountEvents": 3
170+
}
171+
]
172+
```
173+
174+
### Example 2: Modifying an Existing Field
175+
176+
This query adds a field totalStaffCount that sums up the full-time and part-time staff.
177+
178+
```javascript
179+
db.stores.aggregate([
180+
{
181+
$addFields: {
182+
totalStaffCount: {
183+
$add: ["$store.staff.totalStaff.fullTime", "$store.staff.totalStaff.partTime"]
184+
}
185+
}
186+
}
187+
])
188+
```
189+
190+
This query returns the following result:
191+
192+
```json
193+
[
194+
{
195+
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
196+
"store": {
197+
"name": "Downtown Store",
198+
"staff": {
199+
"totalStaff": {
200+
"fullTime": 12,
201+
"partTime": 8
202+
}
203+
}
204+
},
205+
"totalStaffCount": 20
206+
}
207+
]
208+
```
209+
210+
### Example 3: Adding Nested Fields
211+
212+
This query adds a nested field location.coordinates that combines latitude and longitude into an array.
213+
214+
```javascript
215+
db.stores.aggregate([
216+
{
217+
$addFields: {
218+
"store.location.coordinates": ["$store.location.lat", "$store.location.lon"]
219+
}
220+
}
221+
])
222+
```
223+
224+
This query returns the following result:
225+
226+
```json
227+
[
228+
{
229+
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
230+
"store": {
231+
"name": "Downtown Store",
232+
"location": {
233+
"lat": 47.6097,
234+
"lon": -122.3331,
235+
"coordinates": [47.6097, -122.3331]
236+
}
237+
}
238+
}
239+
]
240+
```

0 commit comments

Comments
 (0)