From 58165e2e4cc2a25546f31308a4f6e794c5dc566a Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Mon, 24 Nov 2025 11:59:19 +0000 Subject: [PATCH 01/13] Add python solutions to WHERE challenge --- docs/40-CRUD/1-WHERE.mdx | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/40-CRUD/1-WHERE.mdx b/docs/40-CRUD/1-WHERE.mdx index c798cc7..9288c97 100644 --- a/docs/40-CRUD/1-WHERE.mdx +++ b/docs/40-CRUD/1-WHERE.mdx @@ -130,6 +130,16 @@ Now, translate the following into a MongoDB query. ``` + +
+ ```python + books_with_total_inventory_of_5 = books.find({"totalInventory": 5}) + + for book in books_with_total_inventory_of_5: + print(f"Book Title: {book['title']} - Total Inventory: {book['totalInventory']}") + ``` +
+
@@ -174,6 +184,16 @@ Now, translate the following into a MongoDB query. ``` + +
+ ```python + books_with_more_than_300_pages = books.find({"pages": {"$gt": 300}}) + + for book in books_with_more_than_300_pages: + print(f"Book Title: {book['title']} - Pages: {book['pages']}") + ``` +
+
@@ -216,5 +236,15 @@ Now, translate the following into a MongoDB query. ``` + +
+ ```python + books_with_genre_science_and_more_than_300_pages = books.find({"genres": "Science", "pages": {"$gt": 300}}) + + for book in books_with_genre_science_and_more_than_300_pages: + print(f"Book Title: {book['title']} - Pages: {book['pages']}") + ``` +
+
From 77a707ab37768ab871165e531b31583f0cc858c0 Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Mon, 24 Nov 2025 12:13:23 +0000 Subject: [PATCH 02/13] Add python solutions to SELECT challenge --- docs/40-CRUD/2-SELECT.mdx | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx index df3072f..501b547 100644 --- a/docs/40-CRUD/2-SELECT.mdx +++ b/docs/40-CRUD/2-SELECT.mdx @@ -100,6 +100,17 @@ Here: ``` + +
+ ```python + books_with_title_only = books.find({}, {"title": 1, "_id": 0}).limit(10) + + for book in books_with_title_only: + print(book) + ``` +
+ +
@@ -126,5 +137,15 @@ Here: ``` + +
+ ```python + books_with_genre_history = books.find({"genres": "History"}, {"_id": 0, "authors": 0}).limit(10) + + for book in books_with_genre_history: + print(book) + ``` +
+
From 5723f5576d52e53a2c105cc1b37fcb7da8eb749e Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Mon, 24 Nov 2025 12:16:10 +0000 Subject: [PATCH 03/13] Add python solution to ORDER LIMIT challenge --- docs/40-CRUD/3-ORDER-LIMIT.mdx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/40-CRUD/3-ORDER-LIMIT.mdx b/docs/40-CRUD/3-ORDER-LIMIT.mdx index aa76794..034394b 100644 --- a/docs/40-CRUD/3-ORDER-LIMIT.mdx +++ b/docs/40-CRUD/3-ORDER-LIMIT.mdx @@ -86,5 +86,15 @@ This returns the **top 10 available books** in the "Science Fiction" genre. ``` + +
+ ```python + books_sorted_by_title = books.find({}).sort("title", 1).limit(10) + + for book in books_sorted_by_title: + print(book) + ``` +
+
From 575cd7c8b32e8ead0430834a5cecccd4d13c08ac Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Mon, 24 Nov 2025 12:37:40 +0000 Subject: [PATCH 04/13] Add python syntax highlighting support --- docusaurus.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docusaurus.config.js b/docusaurus.config.js index 25f214a..adfe8b4 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -152,7 +152,7 @@ const config = { prism: { theme: lightCodeTheme, darkTheme: darkCodeTheme, - additionalLanguages: ["powershell", "swift", "kotlin"], + additionalLanguages: ["powershell", "swift", "kotlin", "python"], }, mermaid: { theme: { light: "neutral", dark: "forest" }, From 35ebdc2eabfe3fcec0f9615431e1f1a5a17a3d03 Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Mon, 24 Nov 2025 12:39:08 +0000 Subject: [PATCH 05/13] Add python solution to INSERT DELETE challenge --- docs/40-CRUD/4-INSERT-DELETE.mdx | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/docs/40-CRUD/4-INSERT-DELETE.mdx b/docs/40-CRUD/4-INSERT-DELETE.mdx index 55bccb5..703b69b 100644 --- a/docs/40-CRUD/4-INSERT-DELETE.mdx +++ b/docs/40-CRUD/4-INSERT-DELETE.mdx @@ -136,6 +136,39 @@ DELETE FROM reviews WHERE bookId = '0786222727'; ``` + +
+ ```python + reviews = db.collection("reviews") + reviews.insert_many([ + { + "text": "Thrilling end.", + "rating": 4, + "name": "Mark", + "bookId": "0786222727", + }, + { + "text": "Must read!", + "rating": 5, + "name": "Raj", + "bookId": "0786222727", + }, + { + "text": "Very expensive", + "rating": 3, + "name": "Yun", + "bookId": "0786222727", + }, + { + "text": "Extremely satisfied with the storyline!", + "rating": 5, + "name": "Lisa", + "bookId": "0786222727", + } + ]) + ``` +
+
@@ -159,5 +192,13 @@ DELETE FROM reviews WHERE bookId = '0786222727'; ``` + +
+ ```python + reviews = db.collection("reviews") + reviews.delete_many({"bookId": "0786222727"}) + ``` +
+
From 5301051a676471f600eb8c6c3e7851152c92912f Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Mon, 24 Nov 2025 12:48:21 +0000 Subject: [PATCH 06/13] Add python solution to UPDATE challenge --- docs/40-CRUD/5-UPDATE.mdx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/40-CRUD/5-UPDATE.mdx b/docs/40-CRUD/5-UPDATE.mdx index bf570f5..bd84b50 100644 --- a/docs/40-CRUD/5-UPDATE.mdx +++ b/docs/40-CRUD/5-UPDATE.mdx @@ -106,5 +106,16 @@ Executing the above command will insert a fresh new document in the collection, ``` + +
+ ```python + reviews = db.collection("books") + reviews.update_one( + {"title": "Treasure of the Sun"}, + {"$set": {"pages": 449}} + ) + ``` +
+
From 2322da017c8cdebdbe51c1316f6311f4306915d5 Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Wed, 26 Nov 2025 14:02:45 +0000 Subject: [PATCH 07/13] Resolved conflicts after synced updates --- docs/40-CRUD/2-SELECT.mdx | 43 ++++++------ docs/40-CRUD/3-ORDER-LIMIT.mdx | 19 +++--- docs/40-CRUD/4-INSERT-DELETE.mdx | 114 +++++++++++++++---------------- docs/40-CRUD/5-UPDATE.mdx | 41 +++++------ 4 files changed, 110 insertions(+), 107 deletions(-) diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx index 948ad54..59c6fe4 100644 --- a/docs/40-CRUD/2-SELECT.mdx +++ b/docs/40-CRUD/2-SELECT.mdx @@ -100,17 +100,7 @@ Here: ``` - -
- ```python - books_with_title_only = books.find({}, {"title": 1, "_id": 0}).limit(10) - - for book in books_with_title_only: - print(book) - ``` -
- - +
```csharp var projection = Builders.Projection.Include(b => b.Title).Exclude(b => b.Id); @@ -131,6 +121,16 @@ Here: ```
+ +
+ ```python + books_with_title_only = books.find({}, {"title": 1, "_id": 0}).limit(10) + + for book in books_with_title_only: + print(book) + ``` +
+
@@ -157,16 +157,7 @@ Here: ```
- -
- ```python - books_with_genre_history = books.find({"genres": "History"}, {"_id": 0, "authors": 0}).limit(10) - - for book in books_with_genre_history: - print(book) - ``` -
- +
```csharp var historyGenre = Builders.Filter.AnyEq(b => b.Genres, "History"); @@ -189,5 +180,15 @@ Here: ```
+ +
+ ```python + books_with_genre_history = books.find({"genres": "History"}, {"_id": 0, "authors": 0}).limit(10) + + for book in books_with_genre_history: + print(book) + ``` +
+
diff --git a/docs/40-CRUD/3-ORDER-LIMIT.mdx b/docs/40-CRUD/3-ORDER-LIMIT.mdx index 1489a65..96f40d8 100644 --- a/docs/40-CRUD/3-ORDER-LIMIT.mdx +++ b/docs/40-CRUD/3-ORDER-LIMIT.mdx @@ -86,15 +86,6 @@ This returns the **top 10 available books** in the "Science Fiction" genre. ```
- -
- ```python - books_sorted_by_title = books.find({}).sort("title", 1).limit(10) - - for book in books_sorted_by_title: - print(book) - ``` -
```csharp @@ -108,5 +99,15 @@ This returns the **top 10 available books** in the "Science Fiction" genre. ```
+ +
+ ```python + books_sorted_by_title = books.find({}).sort("title", 1).limit(10) + + for book in books_sorted_by_title: + print(book) + ``` +
+
diff --git a/docs/40-CRUD/4-INSERT-DELETE.mdx b/docs/40-CRUD/4-INSERT-DELETE.mdx index 0176fc1..0a82d16 100644 --- a/docs/40-CRUD/4-INSERT-DELETE.mdx +++ b/docs/40-CRUD/4-INSERT-DELETE.mdx @@ -136,55 +136,54 @@ DELETE FROM reviews WHERE bookId = '0786222727'; ```
+ +
+ ```csharp + var newReviews = new[] + { + new Review { Text = "Thrilling end.", Rating = 4, Name = "Mark", BookId = "0786222727" }, + new Review { Text = "Must read!", Rating = 5, Name = "Raj", BookId = "0786222727" }, + new Review { Text = "Very expensive", Rating = 3, Name = "Yun", BookId = "0786222727" }, + new Review { Text = "Extremely satisfied with the storyline!", Rating = 5, Name = "Lisa", BookId = "0786222727" } + }; + + reviewsCollection.InsertMany(newReviews); + ``` +
+
- ```python - reviews = db.collection("reviews") - reviews.insert_many([ - { - "text": "Thrilling end.", - "rating": 4, - "name": "Mark", - "bookId": "0786222727", - }, - { - "text": "Must read!", - "rating": 5, - "name": "Raj", - "bookId": "0786222727", - }, - { - "text": "Very expensive", - "rating": 3, - "name": "Yun", - "bookId": "0786222727", - }, - { - "text": "Extremely satisfied with the storyline!", - "rating": 5, - "name": "Lisa", - "bookId": "0786222727", - } - ]) - ``` + ```python + reviews = db.collection("reviews") + reviews.insert_many([ + { + "text": "Thrilling end.", + "rating": 4, + "name": "Mark", + "bookId": "0786222727", + }, + { + "text": "Must read!", + "rating": 5, + "name": "Raj", + "bookId": "0786222727", + }, + { + "text": "Very expensive", + "rating": 3, + "name": "Yun", + "bookId": "0786222727", + }, + { + "text": "Extremely satisfied with the storyline!", + "rating": 5, + "name": "Lisa", + "bookId": "0786222727", + } + ]) + ```
-
- -
- ```csharp - var newReviews = new[] - { - new Review { Text = "Thrilling end.", Rating = 4, Name = "Mark", BookId = "0786222727" }, - new Review { Text = "Must read!", Rating = 5, Name = "Raj", BookId = "0786222727" }, - new Review { Text = "Very expensive", Rating = 3, Name = "Yun", BookId = "0786222727" }, - new Review { Text = "Extremely satisfied with the storyline!", Rating = 5, Name = "Lisa", BookId = "0786222727" } - }; - - reviewsCollection.InsertMany(newReviews); - ``` -
-
@@ -208,22 +207,23 @@ DELETE FROM reviews WHERE bookId = '0786222727'; ```
- -
- ```python - reviews = db.collection("reviews") - reviews.delete_many({"bookId": "0786222727"}) - ``` -
- ```csharp - IMongoCollection reviewsCollection = db.GetCollection("reviews"); + ```csharp + IMongoCollection reviewsCollection = db.GetCollection("reviews"); - var deletionResult = reviewsCollection.DeleteMany(r => r.BookId == "0786222727"); + var deletionResult = reviewsCollection.DeleteMany(r => r.BookId == "0786222727"); - Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted."); - ``` + Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted."); + ``` +
+
+ +
+ ```python + reviews = db.collection("reviews") + reviews.delete_many({"bookId": "0786222727"}) + ```
diff --git a/docs/40-CRUD/5-UPDATE.mdx b/docs/40-CRUD/5-UPDATE.mdx index cfc6f93..e236722 100644 --- a/docs/40-CRUD/5-UPDATE.mdx +++ b/docs/40-CRUD/5-UPDATE.mdx @@ -97,14 +97,27 @@ Executing the above command will insert a fresh new document in the collection,
-
- ```js - db.books.updateOne( - {"title": "Treasure of the Sun"}, - {$set: {pages: 449}} - ); - ``` -
+
+ ```js + db.books.updateOne( + {"title": "Treasure of the Sun"}, + {$set: {pages: 449}} + ); + ``` +
+
+ +
+ ```csharp + var filter = Builders.Filter.Eq(b => b.Title, "Treasure of the Sun"); + var update = Builders.Update.Set(b => b.Pages, 449); + + var result = booksCollection.UpdateOne(filter, update); + + // Optionally inspect the outcome + Console.WriteLine($"Matched: {result.MatchedCount}, Modified: {result.ModifiedCount}"); + ``` +
@@ -116,18 +129,6 @@ Executing the above command will insert a fresh new document in the collection, ) ```
- -
- ```csharp - var filter = Builders.Filter.Eq(b => b.Title, "Treasure of the Sun"); - var update = Builders.Update.Set(b => b.Pages, 449); - - var result = booksCollection.UpdateOne(filter, update); - - // Optionally inspect the outcome - Console.WriteLine($"Matched: {result.MatchedCount}, Modified: {result.ModifiedCount}"); - ``` -
From 49cddb4bef4fa72f1dbc254e99664e3302a0ca56 Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Wed, 26 Nov 2025 14:04:49 +0000 Subject: [PATCH 08/13] Add solutions to aggregate chanllenges --- docs/50-aggregation/1-aggregation-intro.mdx | 8 +++ docs/50-aggregation/2-match-project.mdx | 25 +++++++++ docs/50-aggregation/3-sort-limit.mdx | 48 ++++++++++++++++++ docs/50-aggregation/4-group.mdx | 56 +++++++++++++++++++++ docs/50-aggregation/5-lookup.mdx | 17 +++++++ docs/50-aggregation/7-merge.mdx | 36 +++++++++++++ 6 files changed, 190 insertions(+) diff --git a/docs/50-aggregation/1-aggregation-intro.mdx b/docs/50-aggregation/1-aggregation-intro.mdx index a120332..739a270 100644 --- a/docs/50-aggregation/1-aggregation-intro.mdx +++ b/docs/50-aggregation/1-aggregation-intro.mdx @@ -67,6 +67,14 @@ db.books.aggregate([ ]); ``` +``` python +db.books.aggregate([ + { "$match": { "available": { "$gt": 5 } } }, + { "$project": { "title": 1, "available": 1, "_id": 0 } }, + { "$sort": { "available": -1 } }, +]) +``` + --- Next, let's dive into individual stages, starting with `$match` and `$project`. 🚀 diff --git a/docs/50-aggregation/2-match-project.mdx b/docs/50-aggregation/2-match-project.mdx index 9c1915a..b221a7d 100644 --- a/docs/50-aggregation/2-match-project.mdx +++ b/docs/50-aggregation/2-match-project.mdx @@ -140,6 +140,18 @@ db.books.aggregate([ ```
+ +
+ ```python + books_two_copies = books.aggregate([ + { "$match": { "available": { "$gt": 2 } } } + ]) + + for book in books_two_copies: + print(book) + ``` +
+
@@ -191,5 +203,18 @@ db.books.aggregate([ ``` + +
+ ```python + books_two_copies = books.aggregate([ + { "$match": { "available": { "$gt": 2 } } }, + { "$project": { "title": 1, "year": 1, "_id": 0 } } + ]) + + for book in books_two_copies: + print(f"Title: {book['title']} - Publication Year: {book['year']}") + ``` +
+
diff --git a/docs/50-aggregation/3-sort-limit.mdx b/docs/50-aggregation/3-sort-limit.mdx index b30716b..4d933a9 100644 --- a/docs/50-aggregation/3-sort-limit.mdx +++ b/docs/50-aggregation/3-sort-limit.mdx @@ -188,6 +188,54 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre + + + +
+ ```python + most_authors = books.aggregate([ + { "$match": { "year": { "$gt": 2000 } } }, + { "$match": { "authors": { "$exists": True } } }, + { + "$project": { + "title": 1, + "year": 1, + "authors": 1, + "numAuthors": { "$size": "$authors" } + } + }, + { "$sort": { "numAuthors": -1 } }, + { "$limit": 1 } + ]) + + for book in most_authors: + print(book) + ``` +
+
+ + +
+ ```python + most_authors = books.aggregate([ + { "$match": { "year": { "$gt": 2000 } } }, + { "$match": { "authors": { "$exists": True } } }, + { + "$addFields": { + "numAuthors": { "$size": "$authors" } + } + }, + { "$sort": { "numAuthors": -1 } }, + { "$limit": 1 } + ]) + + for book in most_authors: + print(book) + ``` +
+
+
+
diff --git a/docs/50-aggregation/4-group.mdx b/docs/50-aggregation/4-group.mdx index bb30596..0c87984 100644 --- a/docs/50-aggregation/4-group.mdx +++ b/docs/50-aggregation/4-group.mdx @@ -174,6 +174,23 @@ GROUP BY year; ``` + +
+ ```python + books_with_avg_rating = reviews.aggregate([ + { + "$group": { + "_id": "$bookId", + "avgRating": { "$avg": "$rating" } + } + } + ]) + + for book in books_with_avg_rating: + print(book) + ``` +
+
@@ -266,6 +283,45 @@ GROUP BY year; + + + +
+ ```python + reviewers_count = reviews.aggregate([ + { + "$group": { + "_id": "$name", + "totalReviews": { "$sum": 1 } + } + }, + { + "$sort": { "totalReviews": -1 } + } + ]) + + for reviewer in reviewers_count: + print(reviewer) + ``` +
+
+ + +
+ ```python + reviewers_count = reviews.aggregate([ + { + "$sortByCount": "$name" + } + ]) + + for reviewer in reviewers_count: + print(reviewer) + ``` +
+
+
+
diff --git a/docs/50-aggregation/5-lookup.mdx b/docs/50-aggregation/5-lookup.mdx index fe81147..a0eac10 100644 --- a/docs/50-aggregation/5-lookup.mdx +++ b/docs/50-aggregation/5-lookup.mdx @@ -148,5 +148,22 @@ The $lookup operation creates an array within each book document. Using $unwind ``` + +
+ ```python + books_with_reviews = books.aggregate([ + { + "$lookup": + { + "from": "reviews", + "localField": "_id", + "foreignField": "_id.bookId", + "as": "reviews" + } + } + ]) + ``` +
+
diff --git a/docs/50-aggregation/7-merge.mdx b/docs/50-aggregation/7-merge.mdx index cc0d793..8087f7a 100644 --- a/docs/50-aggregation/7-merge.mdx +++ b/docs/50-aggregation/7-merge.mdx @@ -190,6 +190,42 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks); + + + + ```python + author_stats = books.aggregate([ + { "unwind": "$authors" }, + { "$group": { "_id": "$authors.name", "totalBooks": { "$sum": 1 } } }, + { + "$merge": { + "into": "author_stats", + "on": "_id", + "whenMatched": "merge", + "whenNotMatched": "insert" + } + } + ]) + ``` + + + ```python + authors_collection = db.authors + author_stats = authors_collection.aggregate([ + { "$unwind": "$books" }, + { "$group": { "_id": "$name", "totalBooks": { "$sum": 1 } } }, + { "$merge": { + "into": "author_stats", + "on": "_id", + "whenMatched": "merge", + "whenNotMatched": "insert" + } + } + ]) + ``` + + + From 13be89dbaf5cba76be335178ec07e627a84127d4 Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Wed, 26 Nov 2025 14:10:02 +0000 Subject: [PATCH 09/13] Resolve conflict and add python to config again --- docusaurus.config.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docusaurus.config.js b/docusaurus.config.js index a5b73da..3f12e20 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -152,8 +152,7 @@ const config = { prism: { theme: lightCodeTheme, darkTheme: darkCodeTheme, - additionalLanguages: ["powershell", "swift", "kotlin", "python"], - additionalLanguages: ["powershell", "swift", "kotlin", "csharp"], + additionalLanguages: ["powershell", "swift", "kotlin", "python", "csharp"], }, mermaid: { theme: { light: "neutral", dark: "forest" }, From 703307809cb6dd696403dbcaf6fedaab33122e92 Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Fri, 28 Nov 2025 14:39:14 +0000 Subject: [PATCH 10/13] Fix indentation for copying and pasting in cells --- docs/40-CRUD/1-WHERE.mdx | 10 +++++----- docs/40-CRUD/2-SELECT.mdx | 12 ++++++------ docs/40-CRUD/3-ORDER-LIMIT.mdx | 10 +++++----- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/docs/40-CRUD/1-WHERE.mdx b/docs/40-CRUD/1-WHERE.mdx index 02a2765..623de49 100644 --- a/docs/40-CRUD/1-WHERE.mdx +++ b/docs/40-CRUD/1-WHERE.mdx @@ -135,8 +135,8 @@ Now, translate the following into a MongoDB query. ```python books_with_total_inventory_of_5 = books.find({"totalInventory": 5}) - for book in books_with_total_inventory_of_5: - print(f"Book Title: {book['title']} - Total Inventory: {book['totalInventory']}") +for book in books_with_total_inventory_of_5: + print(f"Book Title: {book['title']} - Total Inventory: {book['totalInventory']}") ``` @@ -187,10 +187,10 @@ Now, translate the following into a MongoDB query.
```python - books_with_more_than_300_pages = books.find({"pages": {"$gt": 300}}) + books_with_more_than_300_pages = books.find({"pages": {"$gt": 300}}) - for book in books_with_more_than_300_pages: - print(f"Book Title: {book['title']} - Pages: {book['pages']}") +for book in books_with_more_than_300_pages: + print(f"Book Title: {book['title']} - Pages: {book['pages']}") ```
diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx index 59c6fe4..bba8abe 100644 --- a/docs/40-CRUD/2-SELECT.mdx +++ b/docs/40-CRUD/2-SELECT.mdx @@ -124,10 +124,10 @@ Here:
```python - books_with_title_only = books.find({}, {"title": 1, "_id": 0}).limit(10) + books_with_title_only = books.find({}, {"title": 1, "_id": 0}).limit(10) - for book in books_with_title_only: - print(book) +for book in books_with_title_only: + print(book) ```
@@ -183,10 +183,10 @@ Here:
```python - books_with_genre_history = books.find({"genres": "History"}, {"_id": 0, "authors": 0}).limit(10) + books_with_genre_history = books.find({"genres": "History"}, {"_id": 0, "authors": 0}).limit(10) - for book in books_with_genre_history: - print(book) +for book in books_with_genre_history: + print(book) ```
diff --git a/docs/40-CRUD/3-ORDER-LIMIT.mdx b/docs/40-CRUD/3-ORDER-LIMIT.mdx index 96f40d8..cab1bc5 100644 --- a/docs/40-CRUD/3-ORDER-LIMIT.mdx +++ b/docs/40-CRUD/3-ORDER-LIMIT.mdx @@ -101,12 +101,12 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
- ```python - books_sorted_by_title = books.find({}).sort("title", 1).limit(10) + ```python + books_sorted_by_title = books.find({}).sort("title", 1).limit(10) - for book in books_sorted_by_title: - print(book) - ``` +for book in books_sorted_by_title: + print(book) + ```
From 4f5531fd1863ca95ca1853d7d3ff5df2d300d8f2 Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Fri, 28 Nov 2025 14:41:50 +0000 Subject: [PATCH 11/13] Fix indent and incorrect PyMongo collection access syntax --- docs/40-CRUD/4-INSERT-DELETE.mdx | 8 ++++---- docs/40-CRUD/5-UPDATE.mdx | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/40-CRUD/4-INSERT-DELETE.mdx b/docs/40-CRUD/4-INSERT-DELETE.mdx index 0a82d16..f10be3e 100644 --- a/docs/40-CRUD/4-INSERT-DELETE.mdx +++ b/docs/40-CRUD/4-INSERT-DELETE.mdx @@ -154,8 +154,8 @@ DELETE FROM reviews WHERE bookId = '0786222727';
```python - reviews = db.collection("reviews") - reviews.insert_many([ + reviews = db["reviews"] +reviews.insert_many([ { "text": "Thrilling end.", "rating": 4, @@ -221,8 +221,8 @@ DELETE FROM reviews WHERE bookId = '0786222727';
```python - reviews = db.collection("reviews") - reviews.delete_many({"bookId": "0786222727"}) + reviews = db["reviews"] +reviews.delete_many({"bookId": "0786222727"}) ```
diff --git a/docs/40-CRUD/5-UPDATE.mdx b/docs/40-CRUD/5-UPDATE.mdx index e236722..efe1071 100644 --- a/docs/40-CRUD/5-UPDATE.mdx +++ b/docs/40-CRUD/5-UPDATE.mdx @@ -122,8 +122,8 @@ Executing the above command will insert a fresh new document in the collection,
```python - reviews = db.collection("books") - reviews.update_one( + books = db["books"] +books.update_one( {"title": "Treasure of the Sun"}, {"$set": {"pages": 449}} ) From c9ab72ec479921ee9ca57546a3726a25565ce8ce Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Fri, 28 Nov 2025 14:43:14 +0000 Subject: [PATCH 12/13] Fix indentation for copying and pasting in cells --- docs/50-aggregation/2-match-project.mdx | 24 ++++++++++++------------ docs/50-aggregation/3-sort-limit.mdx | 20 ++++++++++---------- docs/50-aggregation/4-group.mdx | 12 ++++++------ docs/50-aggregation/7-merge.mdx | 2 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/docs/50-aggregation/2-match-project.mdx b/docs/50-aggregation/2-match-project.mdx index b221a7d..e179b9c 100644 --- a/docs/50-aggregation/2-match-project.mdx +++ b/docs/50-aggregation/2-match-project.mdx @@ -142,14 +142,14 @@ db.books.aggregate([
- ```python - books_two_copies = books.aggregate([ + ```python + books_two_copies = books.aggregate([ { "$match": { "available": { "$gt": 2 } } } - ]) + ]) - for book in books_two_copies: - print(book) - ``` +for book in books_two_copies: + print(book) + ```
@@ -205,15 +205,15 @@ db.books.aggregate([
- ```python - books_two_copies = books.aggregate([ + ```python + books_two_copies = books.aggregate([ { "$match": { "available": { "$gt": 2 } } }, { "$project": { "title": 1, "year": 1, "_id": 0 } } - ]) + ]) - for book in books_two_copies: - print(f"Title: {book['title']} - Publication Year: {book['year']}") - ``` +for book in books_two_copies: + print(f"Title: {book['title']} - Publication Year: {book['year']}") + ```
diff --git a/docs/50-aggregation/3-sort-limit.mdx b/docs/50-aggregation/3-sort-limit.mdx index 4d933a9..40f8d35 100644 --- a/docs/50-aggregation/3-sort-limit.mdx +++ b/docs/50-aggregation/3-sort-limit.mdx @@ -192,9 +192,9 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
- ```python - most_authors = books.aggregate([ - { "$match": { "year": { "$gt": 2000 } } }, + ```python + most_authors = books.aggregate([ + { "$match": { "year": { "$gt": 2000 } } }, { "$match": { "authors": { "$exists": True } } }, { "$project": { @@ -208,17 +208,17 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre { "$limit": 1 } ]) - for book in most_authors: - print(book) +for book in most_authors: + print(book) ```
- ```python - most_authors = books.aggregate([ - { "$match": { "year": { "$gt": 2000 } } }, + ```python + most_authors = books.aggregate([ + { "$match": { "year": { "$gt": 2000 } } }, { "$match": { "authors": { "$exists": True } } }, { "$addFields": { @@ -229,8 +229,8 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre { "$limit": 1 } ]) - for book in most_authors: - print(book) +for book in most_authors: + print(book) ```
diff --git a/docs/50-aggregation/4-group.mdx b/docs/50-aggregation/4-group.mdx index 0c87984..ae95a37 100644 --- a/docs/50-aggregation/4-group.mdx +++ b/docs/50-aggregation/4-group.mdx @@ -186,8 +186,8 @@ GROUP BY year; } ]) - for book in books_with_avg_rating: - print(book) +for book in books_with_avg_rating: + print(book) ```
@@ -300,8 +300,8 @@ GROUP BY year; } ]) - for reviewer in reviewers_count: - print(reviewer) +for reviewer in reviewers_count: + print(reviewer) ```
@@ -315,8 +315,8 @@ GROUP BY year; } ]) - for reviewer in reviewers_count: - print(reviewer) +for reviewer in reviewers_count: + print(reviewer) ``` diff --git a/docs/50-aggregation/7-merge.mdx b/docs/50-aggregation/7-merge.mdx index 8087f7a..d89f245 100644 --- a/docs/50-aggregation/7-merge.mdx +++ b/docs/50-aggregation/7-merge.mdx @@ -211,7 +211,7 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks); ```python authors_collection = db.authors - author_stats = authors_collection.aggregate([ +author_stats = authors_collection.aggregate([ { "$unwind": "$books" }, { "$group": { "_id": "$name", "totalBooks": { "$sum": 1 } } }, { "$merge": { From 43cd36af9a47d30ec71198c3c0c34b018491890b Mon Sep 17 00:00:00 2001 From: AfiMaameDufie Date: Fri, 28 Nov 2025 18:27:03 +0000 Subject: [PATCH 13/13] Fix indentation and lint --- docs/40-CRUD/1-WHERE.mdx | 10 ++-- docs/40-CRUD/2-SELECT.mdx | 18 ++++--- docs/40-CRUD/3-ORDER-LIMIT.mdx | 8 ++-- docs/40-CRUD/4-INSERT-DELETE.mdx | 54 ++++++++++----------- docs/40-CRUD/5-UPDATE.mdx | 2 +- docs/50-aggregation/2-match-project.mdx | 8 ++-- docs/50-aggregation/3-sort-limit.mdx | 64 +++++++++++++------------ docs/50-aggregation/4-group.mdx | 44 ++++++++--------- docs/50-aggregation/5-lookup.mdx | 2 +- docs/50-aggregation/7-merge.mdx | 7 +-- 10 files changed, 113 insertions(+), 104 deletions(-) diff --git a/docs/40-CRUD/1-WHERE.mdx b/docs/40-CRUD/1-WHERE.mdx index 623de49..325b053 100644 --- a/docs/40-CRUD/1-WHERE.mdx +++ b/docs/40-CRUD/1-WHERE.mdx @@ -135,8 +135,10 @@ Now, translate the following into a MongoDB query. ```python books_with_total_inventory_of_5 = books.find({"totalInventory": 5}) -for book in books_with_total_inventory_of_5: - print(f"Book Title: {book['title']} - Total Inventory: {book['totalInventory']}") + for book in books_with_total_inventory_of_5: + title = book['title'] + inventory = book['totalInventory'] + print(f"Book Title: {title} - Total Inventory: {inventory}") ``` @@ -189,8 +191,8 @@ for book in books_with_total_inventory_of_5: ```python books_with_more_than_300_pages = books.find({"pages": {"$gt": 300}}) -for book in books_with_more_than_300_pages: - print(f"Book Title: {book['title']} - Pages: {book['pages']}") + for book in books_with_more_than_300_pages: + print(f"Book Title: {book['title']} - Pages: {book['pages']}") ``` diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx index bba8abe..e3fe866 100644 --- a/docs/40-CRUD/2-SELECT.mdx +++ b/docs/40-CRUD/2-SELECT.mdx @@ -124,10 +124,12 @@ Here:
```python - books_with_title_only = books.find({}, {"title": 1, "_id": 0}).limit(10) + books_with_title_only = books.find( + {}, {"title": 1, "_id": 0} + ).limit(10) -for book in books_with_title_only: - print(book) + for book in books_with_title_only: + print(book) ```
@@ -177,16 +179,18 @@ for book in books_with_title_only: { Console.WriteLine("Empty Collection"); } -``` + ```
```python - books_with_genre_history = books.find({"genres": "History"}, {"_id": 0, "authors": 0}).limit(10) + books_with_genre_history = books.find( + {"genres": "History"}, {"_id": 0, "authors": 0} + ).limit(10) -for book in books_with_genre_history: - print(book) + for book in books_with_genre_history: + print(book) ```
diff --git a/docs/40-CRUD/3-ORDER-LIMIT.mdx b/docs/40-CRUD/3-ORDER-LIMIT.mdx index cab1bc5..534588e 100644 --- a/docs/40-CRUD/3-ORDER-LIMIT.mdx +++ b/docs/40-CRUD/3-ORDER-LIMIT.mdx @@ -102,12 +102,12 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
```python - books_sorted_by_title = books.find({}).sort("title", 1).limit(10) + books_sorted_by_title = books.find({}).sort("title", 1).limit(10) -for book in books_sorted_by_title: - print(book) + for book in books_sorted_by_title: + print(book) ``` -
+
diff --git a/docs/40-CRUD/4-INSERT-DELETE.mdx b/docs/40-CRUD/4-INSERT-DELETE.mdx index f10be3e..0af14fc 100644 --- a/docs/40-CRUD/4-INSERT-DELETE.mdx +++ b/docs/40-CRUD/4-INSERT-DELETE.mdx @@ -155,34 +155,34 @@ DELETE FROM reviews WHERE bookId = '0786222727';
```python reviews = db["reviews"] -reviews.insert_many([ - { - "text": "Thrilling end.", - "rating": 4, - "name": "Mark", - "bookId": "0786222727", - }, - { - "text": "Must read!", - "rating": 5, - "name": "Raj", - "bookId": "0786222727", - }, - { - "text": "Very expensive", - "rating": 3, - "name": "Yun", - "bookId": "0786222727", - }, - { - "text": "Extremely satisfied with the storyline!", - "rating": 5, - "name": "Lisa", - "bookId": "0786222727", - } + reviews.insert_many([ + { + "text": "Thrilling end.", + "rating": 4, + "name": "Mark", + "bookId": "0786222727", + }, + { + "text": "Must read!", + "rating": 5, + "name": "Raj", + "bookId": "0786222727", + }, + { + "text": "Very expensive", + "rating": 3, + "name": "Yun", + "bookId": "0786222727", + }, + { + "text": "Extremely satisfied with the storyline!", + "rating": 5, + "name": "Lisa", + "bookId": "0786222727", + } ]) ``` -
+ @@ -222,7 +222,7 @@ reviews.insert_many([
```python reviews = db["reviews"] -reviews.delete_many({"bookId": "0786222727"}) + reviews.delete_many({"bookId": "0786222727"}) ```
diff --git a/docs/40-CRUD/5-UPDATE.mdx b/docs/40-CRUD/5-UPDATE.mdx index efe1071..f25f45f 100644 --- a/docs/40-CRUD/5-UPDATE.mdx +++ b/docs/40-CRUD/5-UPDATE.mdx @@ -123,7 +123,7 @@ Executing the above command will insert a fresh new document in the collection,
```python books = db["books"] -books.update_one( + books.update_one( {"title": "Treasure of the Sun"}, {"$set": {"pages": 449}} ) diff --git a/docs/50-aggregation/2-match-project.mdx b/docs/50-aggregation/2-match-project.mdx index e179b9c..b437b48 100644 --- a/docs/50-aggregation/2-match-project.mdx +++ b/docs/50-aggregation/2-match-project.mdx @@ -144,10 +144,10 @@ db.books.aggregate([
```python books_two_copies = books.aggregate([ - { "$match": { "available": { "$gt": 2 } } } + {"$match": {"available": {"$gt": 2}}} ]) -for book in books_two_copies: + for book in books_two_copies: print(book) ```
@@ -211,10 +211,10 @@ for book in books_two_copies: { "$project": { "title": 1, "year": 1, "_id": 0 } } ]) -for book in books_two_copies: + for book in books_two_copies: print(f"Title: {book['title']} - Publication Year: {book['year']}") ``` -
+ diff --git a/docs/50-aggregation/3-sort-limit.mdx b/docs/50-aggregation/3-sort-limit.mdx index 40f8d35..0febd38 100644 --- a/docs/50-aggregation/3-sort-limit.mdx +++ b/docs/50-aggregation/3-sort-limit.mdx @@ -193,45 +193,47 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
```python - most_authors = books.aggregate([ + most_authors = books.aggregate + ([ { "$match": { "year": { "$gt": 2000 } } }, - { "$match": { "authors": { "$exists": True } } }, - { - "$project": { - "title": 1, - "year": 1, - "authors": 1, - "numAuthors": { "$size": "$authors" } - } - }, - { "$sort": { "numAuthors": -1 } }, - { "$limit": 1 } - ]) - -for book in most_authors: - print(book) - ``` + { "$match": { "authors": { "$exists": True } } }, + { + "$project": { + "title": 1, + "year": 1, + "authors": 1, + "numAuthors": { "$size": "$authors" } + } + }, + { "$sort": { "numAuthors": -1 } }, + { "$limit": 1 } + ]) + + for book in most_authors: + print(book) + ```
```python - most_authors = books.aggregate([ + most_authors = books.aggregate + ([ { "$match": { "year": { "$gt": 2000 } } }, - { "$match": { "authors": { "$exists": True } } }, - { - "$addFields": { - "numAuthors": { "$size": "$authors" } - } - }, - { "$sort": { "numAuthors": -1 } }, - { "$limit": 1 } - ]) - -for book in most_authors: - print(book) - ``` + { "$match": { "authors": { "$exists": True } } }, + { + "$addFields": { + "numAuthors": { "$size": "$authors" } + } + }, + { "$sort": { "numAuthors": -1 } }, + { "$limit": 1 } + ]) + + for book in most_authors: + print(book) + ```
diff --git a/docs/50-aggregation/4-group.mdx b/docs/50-aggregation/4-group.mdx index ae95a37..98f85fe 100644 --- a/docs/50-aggregation/4-group.mdx +++ b/docs/50-aggregation/4-group.mdx @@ -178,16 +178,16 @@ GROUP BY year;
```python books_with_avg_rating = reviews.aggregate([ - { - "$group": { - "_id": "$bookId", - "avgRating": { "$avg": "$rating" } + { + "$group": { + "_id": "$bookId", + "avgRating": {"$avg": "$rating"} + } } - } ]) -for book in books_with_avg_rating: - print(book) + for book in books_with_avg_rating: + print(book) ```
@@ -289,19 +289,19 @@ for book in books_with_avg_rating:
```python reviewers_count = reviews.aggregate([ - { - "$group": { - "_id": "$name", - "totalReviews": { "$sum": 1 } + { + "$group": { + "_id": "$name", + "totalReviews": {"$sum": 1} + } + }, + { + "$sort": {"totalReviews": -1} } - }, - { - "$sort": { "totalReviews": -1 } - } ]) -for reviewer in reviewers_count: - print(reviewer) + for reviewer in reviewers_count: + print(reviewer) ```
@@ -310,13 +310,13 @@ for reviewer in reviewers_count:
```python reviewers_count = reviews.aggregate([ - { - "$sortByCount": "$name" - } + { + "$sortByCount": "$name" + } ]) -for reviewer in reviewers_count: - print(reviewer) + for reviewer in reviewers_count: + print(reviewer) ```
diff --git a/docs/50-aggregation/5-lookup.mdx b/docs/50-aggregation/5-lookup.mdx index a0eac10..369a91e 100644 --- a/docs/50-aggregation/5-lookup.mdx +++ b/docs/50-aggregation/5-lookup.mdx @@ -151,7 +151,7 @@ The $lookup operation creates an array within each book document. Using $unwind
```python - books_with_reviews = books.aggregate([ + books_with_reviews = books.aggregate([ { "$lookup": { diff --git a/docs/50-aggregation/7-merge.mdx b/docs/50-aggregation/7-merge.mdx index d89f245..3788bb5 100644 --- a/docs/50-aggregation/7-merge.mdx +++ b/docs/50-aggregation/7-merge.mdx @@ -211,7 +211,8 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks); ```python authors_collection = db.authors -author_stats = authors_collection.aggregate([ + author_stats = authors_collection.aggregate + ([ { "$unwind": "$books" }, { "$group": { "_id": "$name", "totalBooks": { "$sum": 1 } } }, { "$merge": { @@ -224,8 +225,8 @@ author_stats = authors_collection.aggregate([ ]) ``` - - + +