Skip to content

Commit a9dca87

Browse files
committed
Add reference files
1 parent 3a2123c commit a9dca87

3 files changed

Lines changed: 650 additions & 0 deletions

File tree

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
---
2+
title: $mergeObjects
3+
description: The $mergeObjects operator merges multiple documents into a single document
4+
type: operators
5+
category: object-expression
6+
---
7+
8+
# $mergeObjects
9+
10+
The `$mergeObjects` operator combines multiple documents into a single document. The mergeObjects operation is used in aggregation pipelines to merge fields from different documents or add one or more fields to an existing document. The operator overwrites fields in the target document with fields from the source document when conflicts occur.
11+
12+
## Syntax
13+
14+
```javascript
15+
{
16+
$mergeObjects: [ < document1 > , < document2 > , ...]
17+
}
18+
```
19+
20+
## Parameters
21+
22+
| Parameter | Description |
23+
| --- | --- |
24+
| **`document1, document2`** | The documents to be merged. The documents can be specified as field paths, subdocuments, or constants. |
25+
26+
## Examples
27+
28+
Consider this sample document from the stores collection.
29+
30+
```json
31+
{
32+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
33+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
34+
"location": {
35+
"lat": -89.2384,
36+
"lon": -46.4012
37+
},
38+
"staff": {
39+
"totalStaff": {
40+
"fullTime": 8,
41+
"partTime": 20
42+
}
43+
},
44+
"sales": {
45+
"totalSales": 75670,
46+
"salesByCategory": [
47+
{
48+
"categoryName": "Wine Accessories",
49+
"totalSales": 34440
50+
},
51+
{
52+
"categoryName": "Bitters",
53+
"totalSales": 39496
54+
},
55+
{
56+
"categoryName": "Rum",
57+
"totalSales": 1734
58+
}
59+
]
60+
},
61+
"promotionEvents": [
62+
{
63+
"eventName": "Unbeatable Bargain Bash",
64+
"promotionalDates": {
65+
"startDate": {
66+
"Year": 2024,
67+
"Month": 6,
68+
"Day": 23
69+
},
70+
"endDate": {
71+
"Year": 2024,
72+
"Month": 7,
73+
"Day": 2
74+
}
75+
},
76+
"discounts": [
77+
{
78+
"categoryName": "Whiskey",
79+
"discountPercentage": 7
80+
},
81+
{
82+
"categoryName": "Bitters",
83+
"discountPercentage": 15
84+
},
85+
{
86+
"categoryName": "Brandy",
87+
"discountPercentage": 8
88+
},
89+
{
90+
"categoryName": "Sports Drinks",
91+
"discountPercentage": 22
92+
},
93+
{
94+
"categoryName": "Vodka",
95+
"discountPercentage": 19
96+
}
97+
]
98+
},
99+
{
100+
"eventName": "Steal of a Deal Days",
101+
"promotionalDates": {
102+
"startDate": {
103+
"Year": 2024,
104+
"Month": 9,
105+
"Day": 21
106+
},
107+
"endDate": {
108+
"Year": 2024,
109+
"Month": 9,
110+
"Day": 29
111+
}
112+
},
113+
"discounts": [
114+
{
115+
"categoryName": "Organic Wine",
116+
"discountPercentage": 19
117+
},
118+
{
119+
"categoryName": "White Wine",
120+
"discountPercentage": 20
121+
},
122+
{
123+
"categoryName": "Sparkling Wine",
124+
"discountPercentage": 19
125+
},
126+
{
127+
"categoryName": "Whiskey",
128+
"discountPercentage": 17
129+
},
130+
{
131+
"categoryName": "Vodka",
132+
"discountPercentage": 23
133+
}
134+
]
135+
}
136+
]
137+
}
138+
```
139+
140+
### Example 1 - Merging documents as an accumulator to group documents by the sales subdocument
141+
142+
The query merges all sales subdocuments per city for a specific company.
143+
144+
```javascript
145+
db.stores.aggregate([
146+
{
147+
$match: {
148+
company: "Fourth Coffee"
149+
}
150+
},
151+
{
152+
$group: {
153+
_id: "$city",
154+
mergedSales: {
155+
$mergeObjects: "$sales"
156+
}
157+
}
158+
},
159+
{
160+
$limit: 2 // returns only the first 3 grouped cities
161+
}
162+
])
163+
```
164+
165+
The first two results returned by this query are:
166+
167+
```json
168+
[
169+
{
170+
"_id": "Jalonborough",
171+
"mergedSales": {
172+
"totalSales": 45747,
173+
"salesByCategory": [
174+
{
175+
"categoryName": "Bucket Bags",
176+
"totalSales": 45747
177+
}
178+
]
179+
}
180+
},
181+
{
182+
"_id": "Port Vladimir",
183+
"mergedSales": {
184+
"totalSales": 32000,
185+
"salesByCategory": [
186+
{
187+
"categoryName": "DJ Speakers",
188+
"totalSales": 24989
189+
},
190+
{
191+
"categoryName": "DJ Cables",
192+
"totalSales": 7011
193+
}
194+
]
195+
}
196+
}
197+
]
198+
```

0 commit comments

Comments
 (0)