@@ -13,10 +13,10 @@ The `$merge` stage enables you to store aggregation results into a different col
13
13
14
14
### ** Key features:**
15
15
16
- ✔️ Inserts new documents if they don’t exist
17
- ✔️ Updates existing documents based on ` _id ` or a specified field
18
- ✔️ Can replace, merge, or discard duplicate records
19
- ✔️ Useful for ** ETL workflows, reporting tables, and maintaining summary data**
16
+ - ✔️ Inserts new documents if they don’t exist
17
+ - ✔️ Updates existing documents based on ` _id ` or a specified field
18
+ - ✔️ Can replace, merge, or discard duplicate records
19
+ - ✔️ Useful for ** ETL workflows, reporting tables, and maintaining summary data**
20
20
21
21
---
22
22
@@ -51,8 +51,8 @@ The `$merge` stage enables you to store aggregation results into a different col
51
51
52
52
``` js
53
53
db .books .aggregate ([
54
- { $unwind: " $genres " },
55
- { $group: { _id: " $genres " , totalBooks: { $sum: 1 } } },
54
+ { $unwind: " $genre " },
55
+ { $group: { _id: " $genre.genreId " , totalBooks: { $sum: 1 } } },
56
56
{
57
57
$merge: {
58
58
into: " genre_summary" ,
@@ -99,18 +99,18 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
99
99
<Tabs groupId = " aggregations" >
100
100
<TabItem value = " books" label = " through 'books' collection" >
101
101
``` js
102
- db .books .aggregate ([
103
- { $unwind: " $authors" },
104
- { $group: { _id: " $authors.name" , totalBooks: { $sum: 1 } } },
105
- {
106
- $merge: {
107
- into: " author_stats" ,
108
- on: " _id" ,
109
- whenMatched: " merge" ,
110
- whenNotMatched: " insert" ,
111
- },
102
+ db .books .aggregate ([
103
+ { $unwind: " $authors" },
104
+ { $group: { _id: " $authors.name" , totalBooks: { $sum: 1 } } },
105
+ {
106
+ $merge: {
107
+ into: " author_stats" ,
108
+ on: " _id" ,
109
+ whenMatched: " merge" ,
110
+ whenNotMatched: " insert" ,
112
111
},
113
- ]);
112
+ },
113
+ ]);
114
114
```
115
115
</TabItem >
116
116
0 commit comments