Skip to content

Commit 3d82fbf

Browse files
Fixed query typo
1 parent 2702f88 commit 3d82fbf

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

docs/50-aggregation/4-group.mdx

+2-4
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ db.books.aggregate([
9393

9494
## 👐 Example 3: Find the total number of pages published every year.
9595

96-
9796
### **MongoDB query**
9897

9998
```js
@@ -130,7 +129,6 @@ GROUP BY year;
130129

131130
## 👐 Challenge
132131

133-
134132
### 👐 1. Find the average book rating of all books
135133

136134
<details>
@@ -157,7 +155,7 @@ GROUP BY year;
157155
- $group with $sort
158156
- $sortByCount
159157
<Tabs groupId="aggregations">
160-
<TabItem value="atlas" label="$group with $sum + $sort">
158+
<TabItem value="$group with $sum + $sort" label="$group with $sum + $sort">
161159
```js
162160
db.reviews.aggregate([
163161
{
@@ -175,7 +173,7 @@ GROUP BY year;
175173
```
176174
</TabItem>
177175

178-
<TabItem value="mongodb-shell" label="$sortByCount">
176+
<TabItem value="sortByCount" label="$sortByCount">
179177
```js
180178
db.reviews.aggregate([
181179
{

docs/50-aggregation/7-merge.mdx

+37-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import Tabs from "@theme/Tabs";
2+
import TabItem from "@theme/TabItem";
3+
14
# 🦸 $merge
25

36
In MongoDB, the **$merge** stage allows you to **write the results of an aggregation pipeline into a new or existing collection**. This is similar to the concept of "INSERT INTO ... SELECT" or "MERGE INTO" in SQL databases.
@@ -89,18 +92,45 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
8992

9093
<details>
9194
<summary>Answer</summary>
92-
```js
93-
db.books.aggregate([
94-
{ $unwind: "$authors" },
95-
{ $group: { _id: "$authors.name", totalBooks: { $sum: 1 } } },
95+
<div>
96+
There are 2 ways to solve this-
97+
- through `books` collection
98+
- through `authors` collection
99+
<Tabs groupId="aggregations">
100+
<TabItem value="books" label="through 'books' collection">
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+
},
112+
},
113+
]);
114+
```
115+
</TabItem>
116+
117+
<TabItem value="authors" label="through 'authors' collection">
118+
```js
119+
db.authors.aggregate([
120+
{ $unwind: "$books" },
121+
{ $group: { _id: "$name", totalBooks: { $sum: 1 } } },
96122
{
97123
$merge: {
98124
into: "author_stats",
99125
on: "_id",
100126
whenMatched: "merge",
101-
whenNotMatched: "insert",
102-
},
103-
},
127+
whenNotMatched: "insert"
128+
}
129+
}
104130
]);
105131
```
132+
</TabItem>
133+
134+
</Tabs>
135+
</div>
106136
</details>

0 commit comments

Comments
 (0)