Skip to content

Commit ed12f9b

Browse files
committedFeb 21, 2025·
Fixed Challenge heading
1 parent b002331 commit ed12f9b

File tree

3 files changed

+57
-38
lines changed

3 files changed

+57
-38
lines changed
 

‎docs/50-aggregation/3-sort-limit.mdx

+41-6
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
# 👐 $sort and $limit
25

36
## 🔹 $sort → Sorting results
@@ -67,24 +70,54 @@ SELECT * FROM books ORDER BY timestamp DESC LIMIT 5;
6770

6871
---
6972

70-
### 👐 Challenge
73+
## 👐 Challenge
7174

7275
## 👐 1. After the year 2010, which book has the most number of authors?
7376

7477
<details>
7578
<summary>Answer</summary>
7679
<div>
77-
```js
80+
There are 2 ways to solve this-
81+
- $project
82+
- $addFields
83+
84+
:::info
85+
Learn [when to use $addFields over $project](https://www.practical-mongodb-aggregations.com/guides/project.html?highlight=%24project#when-to-use-project)
86+
:::
87+
88+
<Tabs groupId="aggregations">
89+
<TabItem value="mongodb-shell" label="Using $project">
90+
```js
7891
db.books.aggregate([
92+
{
93+
$match: { year: {$gt: 2010}}
94+
},
95+
{
96+
$project: {
97+
title: 1,
98+
authors: 1,
99+
numAuthors: {$size: "$authors"},
100+
_id: 0
101+
}
102+
},
103+
{
104+
$sort: { "numAuthors": -1}
105+
},
106+
{
107+
$limit: 1
108+
}
109+
]);
110+
```
111+
</TabItem>
112+
<TabItem value="atlas" label="Using $addFields">
113+
```js
114+
db.books.aggregate([
79115
{
80116
$match: { year: {$gt: 2010}}
81117
},
82118
{
83-
$project: {
84-
title: 1,
85-
authors: 1,
119+
$addFields: {
86120
numAuthors: {$size: "$authors"},
87-
_id: 0
88121
}
89122
},
90123
{
@@ -95,5 +128,7 @@ SELECT * FROM books ORDER BY timestamp DESC LIMIT 5;
95128
}
96129
]);
97130
```
131+
</TabItem>
132+
</Tabs>
98133
</div>
99134
</details>

‎docs/50-aggregation/4-group.mdx

+2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ GROUP BY year;
152152

153153
<details>
154154
<summary>Answer</summary>
155+
<div>
155156
There are 2 ways to solve this-
156157
- $group with $sort
157158
- $sortByCount
@@ -185,4 +186,5 @@ GROUP BY year;
185186
</TabItem>
186187

187188
</Tabs>
189+
</div>
188190
</details>

‎docs/50-aggregation/7-CREATE-VIEW.mdx

+14-32
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,13 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
7272

7373
---
7474

75-
## **🔹 Example 2: Maintaining an author summary table**
75+
## 👐 Challenge
7676

77-
👉 We want to create an **author_stats** collection with the total number of books written by each author.
77+
### **🔹 1: Maintaining an Author Summary Table**
7878

79-
```js
80-
db.books.aggregate([
81-
{ $unwind: "$authors" },
82-
{ $group: { _id: "$authors.name", totalBooks: { $sum: 1 } } },
83-
{
84-
$merge: {
85-
into: "author_stats",
86-
on: "_id",
87-
whenMatched: "merge",
88-
whenNotMatched: "insert",
89-
},
90-
},
91-
]);
92-
```
79+
👉 We want to create an **author_stats** collection with the total number of books written by each author.
9380

94-
### **SQL equivalent**
81+
### Hint: SQL equivalent
9582

9683
```sql
9784
INSERT INTO author_stats (authorName, totalBooks)
@@ -100,25 +87,20 @@ GROUP BY authors.name
10087
ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
10188
```
10289

103-
---
104-
105-
## 👐 Challenge
106-
107-
👐 Create a `top_rated_books` collection that stores books with an average rating above 4.5.
108-
10990
<details>
11091
<summary>Answer</summary>
111-
```js
92+
```js
11293
db.books.aggregate([
94+
{ $unwind: "$authors" },
95+
{ $group: { _id: "$authors.name", totalBooks: { $sum: 1 } } },
11396
{
114-
$lookup:
115-
{
116-
from: "reviews",
117-
localField: "_id",
118-
foreignField: "bookId",
119-
as: "reviews"
120-
}
121-
}
97+
$merge: {
98+
into: "author_stats",
99+
on: "_id",
100+
whenMatched: "merge",
101+
whenNotMatched: "insert",
102+
},
103+
},
122104
]);
123105
```
124106
</details>

0 commit comments

Comments
 (0)
Please sign in to comment.