Skip to content

Commit 49cddb4

Browse files
committed
Add solutions to aggregate chanllenges
1 parent 2322da0 commit 49cddb4

File tree

6 files changed

+190
-0
lines changed

6 files changed

+190
-0
lines changed

docs/50-aggregation/1-aggregation-intro.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ db.books.aggregate([
6767
]);
6868
```
6969

70+
``` python
71+
db.books.aggregate([
72+
{ "$match": { "available": { "$gt": 5 } } },
73+
{ "$project": { "title": 1, "available": 1, "_id": 0 } },
74+
{ "$sort": { "available": -1 } },
75+
])
76+
```
77+
7078
---
7179

7280
Next, let's dive into individual stages, starting with `$match` and `$project`. 🚀

docs/50-aggregation/2-match-project.mdx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ db.books.aggregate([
140140
```
141141
</div>
142142
</TabItem>
143+
<TabItem value="python" label="Python">
144+
<div>
145+
```python
146+
books_two_copies = books.aggregate([
147+
{ "$match": { "available": { "$gt": 2 } } }
148+
])
149+
150+
for book in books_two_copies:
151+
print(book)
152+
```
153+
</div>
154+
</TabItem>
143155
</Tabs>
144156
</details>
145157

@@ -191,5 +203,18 @@ db.books.aggregate([
191203
```
192204
</div>
193205
</TabItem>
206+
<TabItem value="python" label="Python">
207+
<div>
208+
```python
209+
books_two_copies = books.aggregate([
210+
{ "$match": { "available": { "$gt": 2 } } },
211+
{ "$project": { "title": 1, "year": 1, "_id": 0 } }
212+
])
213+
214+
for book in books_two_copies:
215+
print(f"Title: {book['title']} - Publication Year: {book['year']}")
216+
```
217+
</div>
218+
</TabItem>
194219
</Tabs>
195220
</details>

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,54 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
188188
</TabItem>
189189
</Tabs>
190190
</TabItem>
191+
<TabItem value="python" label="Python">
192+
<Tabs groupId="aggregations-python" defaultValue="addFields">
193+
<TabItem value="project" label="Using $project">
194+
<div>
195+
```python
196+
most_authors = books.aggregate([
197+
{ "$match": { "year": { "$gt": 2000 } } },
198+
{ "$match": { "authors": { "$exists": True } } },
199+
{
200+
"$project": {
201+
"title": 1,
202+
"year": 1,
203+
"authors": 1,
204+
"numAuthors": { "$size": "$authors" }
205+
}
206+
},
207+
{ "$sort": { "numAuthors": -1 } },
208+
{ "$limit": 1 }
209+
])
210+
211+
for book in most_authors:
212+
print(book)
213+
```
214+
</div>
215+
</TabItem>
216+
217+
<TabItem value="addFields" label="Using $addFields">
218+
<div>
219+
```python
220+
most_authors = books.aggregate([
221+
{ "$match": { "year": { "$gt": 2000 } } },
222+
{ "$match": { "authors": { "$exists": True } } },
223+
{
224+
"$addFields": {
225+
"numAuthors": { "$size": "$authors" }
226+
}
227+
},
228+
{ "$sort": { "numAuthors": -1 } },
229+
{ "$limit": 1 }
230+
])
231+
232+
for book in most_authors:
233+
print(book)
234+
```
235+
</div>
236+
</TabItem>
237+
</Tabs>
238+
</TabItem>
191239
</Tabs>
192240
</div>
193241
</details>

docs/50-aggregation/4-group.mdx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ GROUP BY year;
174174
```
175175
</div>
176176
</TabItem>
177+
<TabItem value="python" label="Python">
178+
<div>
179+
```python
180+
books_with_avg_rating = reviews.aggregate([
181+
{
182+
"$group": {
183+
"_id": "$bookId",
184+
"avgRating": { "$avg": "$rating" }
185+
}
186+
}
187+
])
188+
189+
for book in books_with_avg_rating:
190+
print(book)
191+
```
192+
</div>
193+
</TabItem>
177194
</Tabs>
178195
</details>
179196

@@ -266,6 +283,45 @@ GROUP BY year;
266283
</TabItem>
267284
</Tabs>
268285
</TabItem>
286+
<TabItem value="python" label="Python">
287+
<Tabs groupId="aggregations-python" defaultValue="$group">
288+
<TabItem value="$group" label="$group with $sum + $sort">
289+
<div>
290+
```python
291+
reviewers_count = reviews.aggregate([
292+
{
293+
"$group": {
294+
"_id": "$name",
295+
"totalReviews": { "$sum": 1 }
296+
}
297+
},
298+
{
299+
"$sort": { "totalReviews": -1 }
300+
}
301+
])
302+
303+
for reviewer in reviewers_count:
304+
print(reviewer)
305+
```
306+
</div>
307+
</TabItem>
308+
309+
<TabItem value="$sortByCount" label="$sortByCount">
310+
<div>
311+
```python
312+
reviewers_count = reviews.aggregate([
313+
{
314+
"$sortByCount": "$name"
315+
}
316+
])
317+
318+
for reviewer in reviewers_count:
319+
print(reviewer)
320+
```
321+
</div>
322+
</TabItem>
323+
</Tabs>
324+
</TabItem>
269325
</Tabs>
270326
</div>
271327
</details>

docs/50-aggregation/5-lookup.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,22 @@ The $lookup operation creates an array within each book document. Using $unwind
148148
```
149149
</div>
150150
</TabItem>
151+
<TabItem value="python" label="Python">
152+
<div>
153+
```python
154+
books_with_reviews = books.aggregate([
155+
{
156+
"$lookup":
157+
{
158+
"from": "reviews",
159+
"localField": "_id",
160+
"foreignField": "_id.bookId",
161+
"as": "reviews"
162+
}
163+
}
164+
])
165+
```
166+
</div>
167+
</TabItem>
151168
</Tabs>
152169
</details>

docs/50-aggregation/7-merge.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,42 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
190190
</TabItem>
191191
</Tabs>
192192
</TabItem>
193+
<TabItem value="python" label="Python">
194+
<Tabs groupId="merge-python" defaultValue="books">
195+
<TabItem value="books" label="through books collection">
196+
```python
197+
author_stats = books.aggregate([
198+
{ "unwind": "$authors" },
199+
{ "$group": { "_id": "$authors.name", "totalBooks": { "$sum": 1 } } },
200+
{
201+
"$merge": {
202+
"into": "author_stats",
203+
"on": "_id",
204+
"whenMatched": "merge",
205+
"whenNotMatched": "insert"
206+
}
207+
}
208+
])
209+
```
210+
</TabItem>
211+
<TabItem value="authors" label="through authors collection">
212+
```python
213+
authors_collection = db.authors
214+
author_stats = authors_collection.aggregate([
215+
{ "$unwind": "$books" },
216+
{ "$group": { "_id": "$name", "totalBooks": { "$sum": 1 } } },
217+
{ "$merge": {
218+
"into": "author_stats",
219+
"on": "_id",
220+
"whenMatched": "merge",
221+
"whenNotMatched": "insert"
222+
}
223+
}
224+
])
225+
```
226+
</TabItem>
227+
</Tabs>
228+
</TabItem>
193229
</Tabs>
194230
</div>
195231
</details>

0 commit comments

Comments
 (0)