@@ -13,10 +13,10 @@ The `$merge` stage enables you to store aggregation results into a different col
1313
1414### ** Key features:**
1515
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**
2020
2121---
2222
@@ -51,8 +51,8 @@ The `$merge` stage enables you to store aggregation results into a different col
5151
5252``` js
5353db .books .aggregate ([
54- { $unwind: " $genres " },
55- { $group: { _id: " $genres " , totalBooks: { $sum: 1 } } },
54+ { $unwind: " $genre " },
55+ { $group: { _id: " $genre.genreId " , totalBooks: { $sum: 1 } } },
5656 {
5757 $merge: {
5858 into: " genre_summary" ,
@@ -99,18 +99,18 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
9999 <Tabs groupId = " aggregations" >
100100 <TabItem value = " books" label = " through 'books' collection" >
101101 ``` 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" ,
112111 },
113- ]);
112+ },
113+ ]);
114114 ```
115115 </TabItem >
116116
0 commit comments