Skip to content

Commit 7d02a3d

Browse files
authored
Merge pull request #32 from AfiMaameDufie/add-python-solutions
Add python solutions to CRUD workshop challenges
2 parents 7bf73fd + a152cc7 commit 7d02a3d

File tree

12 files changed

+361
-43
lines changed

12 files changed

+361
-43
lines changed

docs/40-CRUD/1-WHERE.mdx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ Now, translate the following into a MongoDB query.
130130
```
131131
</div>
132132
</TabItem>
133+
<TabItem value="python" label="Python">
134+
<div>
135+
```python
136+
books_with_total_inventory_of_5 = books.find({"totalInventory": 5})
137+
138+
for book in books_with_total_inventory_of_5:
139+
title = book['title']
140+
inventory = book['totalInventory']
141+
print(f"Book Title: {title} - Total Inventory: {inventory}")
142+
```
143+
</div>
144+
</TabItem>
133145
</Tabs>
134146

135147
</details>
@@ -174,6 +186,16 @@ Now, translate the following into a MongoDB query.
174186
```
175187
</div>
176188
</TabItem>
189+
<TabItem value="python" label="Python">
190+
<div>
191+
```python
192+
books_with_more_than_300_pages = books.find({"pages": {"$gt": 300}})
193+
194+
for book in books_with_more_than_300_pages:
195+
print(f"Book Title: {book['title']} - Pages: {book['pages']}")
196+
```
197+
</div>
198+
</TabItem>
177199
</Tabs>
178200
</details>
179201

@@ -225,5 +247,15 @@ Now, translate the following into a MongoDB query.
225247
```
226248
</div>
227249
</TabItem>
250+
<TabItem value="python" label="Python">
251+
<div>
252+
```python
253+
books_with_genre_science_and_more_than_300_pages = books.find({"genres": "Science", "pages": {"$gt": 300}})
254+
255+
for book in books_with_genre_science_and_more_than_300_pages:
256+
print(f"Book Title: {book['title']} - Pages: {book['pages']}")
257+
```
258+
</div>
259+
</TabItem>
228260
</Tabs>
229261
</details>

docs/40-CRUD/2-SELECT.mdx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Here:
100100
```
101101
</div>
102102
</TabItem>
103-
<TabItem value="csharp" label="C#">
103+
<TabItem value="csharp" label="C#">
104104
<div>
105105
```csharp
106106
var projection = Builders<Book>.Projection.Include(b => b.Title).Exclude(b => b.Id);
@@ -121,6 +121,18 @@ Here:
121121
```
122122
</div>
123123
</TabItem>
124+
<TabItem value="python" label="Python">
125+
<div>
126+
```python
127+
books_with_title_only = books.find(
128+
{}, {"title": 1, "_id": 0}
129+
).limit(10)
130+
131+
for book in books_with_title_only:
132+
print(book)
133+
```
134+
</div>
135+
</TabItem>
124136
</Tabs>
125137
</details>
126138

@@ -147,7 +159,7 @@ Here:
147159
```
148160
</div>
149161
</TabItem>
150-
<TabItem value="csharp" label="C#">
162+
<TabItem value="csharp" label="C#">
151163
<div>
152164
```csharp
153165
var historyGenre = Builders<Book>.Filter.AnyEq(b => b.Genres, "History");
@@ -167,7 +179,19 @@ Here:
167179
{
168180
Console.WriteLine("Empty Collection");
169181
}
170-
```
182+
```
183+
</div>
184+
</TabItem>
185+
<TabItem value="python" label="Python">
186+
<div>
187+
```python
188+
books_with_genre_history = books.find(
189+
{"genres": "History"}, {"_id": 0, "authors": 0}
190+
).limit(10)
191+
192+
for book in books_with_genre_history:
193+
print(book)
194+
```
171195
</div>
172196
</TabItem>
173197
</Tabs>

docs/40-CRUD/3-ORDER-LIMIT.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,15 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
9999
```
100100
</div>
101101
</TabItem>
102+
<TabItem value="python" label="Python">
103+
<div>
104+
```python
105+
books_sorted_by_title = books.find({}).sort("title", 1).limit(10)
106+
107+
for book in books_sorted_by_title:
108+
print(book)
109+
```
110+
</div>
111+
</TabItem>
102112
</Tabs>
103113
</details>

docs/40-CRUD/4-INSERT-DELETE.mdx

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,55 @@ DELETE FROM reviews WHERE bookId = '0786222727';
135135
]);
136136
```
137137
</div>
138+
</TabItem>
139+
<TabItem value="csharp" label="C#">
140+
<div>
141+
```csharp
142+
var newReviews = new[]
143+
{
144+
new Review { Text = "Thrilling end.", Rating = 4, Name = "Mark", BookId = "0786222727" },
145+
new Review { Text = "Must read!", Rating = 5, Name = "Raj", BookId = "0786222727" },
146+
new Review { Text = "Very expensive", Rating = 3, Name = "Yun", BookId = "0786222727" },
147+
new Review { Text = "Extremely satisfied with the storyline!", Rating = 5, Name = "Lisa", BookId = "0786222727" }
148+
};
149+
150+
reviewsCollection.InsertMany(newReviews);
151+
```
152+
</div>
138153
</TabItem>
139-
<TabItem value="csharp" label="C#">
140-
<div>
141-
```csharp
142-
var newReviews = new[]
154+
<TabItem value="python" label="Python">
155+
<div>
156+
```python
157+
reviews = db["reviews"]
158+
reviews.insert_many([
143159
{
144-
new Review { Text = "Thrilling end.", Rating = 4, Name = "Mark", BookId = "0786222727" },
145-
new Review { Text = "Must read!", Rating = 5, Name = "Raj", BookId = "0786222727" },
146-
new Review { Text = "Very expensive", Rating = 3, Name = "Yun", BookId = "0786222727" },
147-
new Review { Text = "Extremely satisfied with the storyline!", Rating = 5, Name = "Lisa", BookId = "0786222727" }
148-
};
149-
150-
reviewsCollection.InsertMany(newReviews);
151-
```
152-
</div>
153-
</TabItem>
160+
"text": "Thrilling end.",
161+
"rating": 4,
162+
"name": "Mark",
163+
"bookId": "0786222727",
164+
},
165+
{
166+
"text": "Must read!",
167+
"rating": 5,
168+
"name": "Raj",
169+
"bookId": "0786222727",
170+
},
171+
{
172+
"text": "Very expensive",
173+
"rating": 3,
174+
"name": "Yun",
175+
"bookId": "0786222727",
176+
},
177+
{
178+
"text": "Extremely satisfied with the storyline!",
179+
"rating": 5,
180+
"name": "Lisa",
181+
"bookId": "0786222727",
182+
}
183+
])
184+
```
185+
</div>
186+
</TabItem>
154187
</Tabs>
155188
</details>
156189

@@ -176,13 +209,21 @@ DELETE FROM reviews WHERE bookId = '0786222727';
176209
</TabItem>
177210
<TabItem value="csharp" label="C#">
178211
<div>
179-
```csharp
180-
IMongoCollection<Review> reviewsCollection = db.GetCollection<Review>("reviews");
212+
```csharp
213+
IMongoCollection<Review> reviewsCollection = db.GetCollection<Review>("reviews");
181214

182-
var deletionResult = reviewsCollection.DeleteMany(r => r.BookId == "0786222727");
215+
var deletionResult = reviewsCollection.DeleteMany(r => r.BookId == "0786222727");
183216

184-
Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted.");
185-
```
217+
Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted.");
218+
```
219+
</div>
220+
</TabItem>
221+
<TabItem value="python" label="Python">
222+
<div>
223+
```python
224+
reviews = db["reviews"]
225+
reviews.delete_many({"bookId": "0786222727"})
226+
```
186227
</div>
187228
</TabItem>
188229
</Tabs>

docs/40-CRUD/5-UPDATE.mdx

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,38 @@ Executing the above command will insert a fresh new document in the collection,
9797
</div>
9898
</TabItem>
9999
<TabItem value="mongosh" label="mongosh">
100-
<div>
101-
```js
102-
db.books.updateOne(
103-
{"title": "Treasure of the Sun"},
104-
{$set: {pages: 449}}
105-
);
106-
```
107-
</div>
100+
<div>
101+
```js
102+
db.books.updateOne(
103+
{"title": "Treasure of the Sun"},
104+
{$set: {pages: 449}}
105+
);
106+
```
107+
</div>
108108
</TabItem>
109-
<TabItem value="csharp" label="C#">
110-
<div>
111-
```csharp
112-
var filter = Builders<Book>.Filter.Eq(b => b.Title, "Treasure of the Sun");
113-
var update = Builders<Book>.Update.Set(b => b.Pages, 449);
114-
115-
var result = booksCollection.UpdateOne(filter, update);
116-
117-
// Optionally inspect the outcome
118-
Console.WriteLine($"Matched: {result.MatchedCount}, Modified: {result.ModifiedCount}");
119-
```
120-
</div>
109+
<TabItem value="csharp" label="C#">
110+
<div>
111+
```csharp
112+
var filter = Builders<Book>.Filter.Eq(b => b.Title, "Treasure of the Sun");
113+
var update = Builders<Book>.Update.Set(b => b.Pages, 449);
114+
115+
var result = booksCollection.UpdateOne(filter, update);
116+
117+
// Optionally inspect the outcome
118+
Console.WriteLine($"Matched: {result.MatchedCount}, Modified: {result.ModifiedCount}");
119+
```
120+
</div>
121+
</TabItem>
122+
<TabItem value="python" label="Python">
123+
<div>
124+
```python
125+
books = db["books"]
126+
books.update_one(
127+
{"title": "Treasure of the Sun"},
128+
{"$set": {"pages": 449}}
129+
)
130+
```
131+
</div>
121132
</TabItem>
122133
</Tabs>
123134
</details>

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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,63 @@ 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+
([
198+
{
199+
"$match": {
200+
"year": { "$gt": 2000 },
201+
"authors": { "$exists": True }
202+
}
203+
},
204+
{
205+
"$project": {
206+
"title": 1,
207+
"year": 1,
208+
"authors": 1,
209+
"numAuthors": { "$size": "$authors" }
210+
}
211+
},
212+
{ "$sort": { "numAuthors": -1 } },
213+
{ "$limit": 1 }
214+
])
215+
216+
for book in most_authors:
217+
print(book)
218+
```
219+
</div>
220+
</TabItem>
221+
222+
<TabItem value="addFields" label="Using $addFields">
223+
<div>
224+
```python
225+
most_authors = books.aggregate
226+
([
227+
{
228+
"$match":
229+
{ "year": { "$gt": 2000 } },
230+
{ "authors": { "$exists": True } }
231+
},
232+
{
233+
"$addFields": {
234+
"numAuthors": { "$size": "$authors" }
235+
}
236+
},
237+
{ "$sort": { "numAuthors": -1 } },
238+
{ "$limit": 1 }
239+
])
240+
241+
for book in most_authors:
242+
print(book)
243+
```
244+
</div>
245+
</TabItem>
246+
</Tabs>
247+
</TabItem>
191248
</Tabs>
192249
</div>
193250
</details>

0 commit comments

Comments
 (0)