diff --git a/docs/40-CRUD/1-WHERE.mdx b/docs/40-CRUD/1-WHERE.mdx
index 751bee8..325b053 100644
--- a/docs/40-CRUD/1-WHERE.mdx
+++ b/docs/40-CRUD/1-WHERE.mdx
@@ -130,6 +130,18 @@ 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:
+ title = book['title']
+ inventory = book['totalInventory']
+ print(f"Book Title: {title} - Total Inventory: {inventory}")
+ ```
+
+
@@ -174,6 +186,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']}")
+ ```
+
+
@@ -225,5 +247,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']}")
+ ```
+
+
diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx
index c80fcd3..e3fe866 100644
--- a/docs/40-CRUD/2-SELECT.mdx
+++ b/docs/40-CRUD/2-SELECT.mdx
@@ -100,7 +100,7 @@ Here:
```
-
+
```csharp
var projection = Builders.Projection.Include(b => b.Title).Exclude(b => b.Id);
@@ -121,6 +121,18 @@ Here:
```
+
+
+ ```python
+ books_with_title_only = books.find(
+ {}, {"title": 1, "_id": 0}
+ ).limit(10)
+
+ for book in books_with_title_only:
+ print(book)
+ ```
+
+
@@ -147,7 +159,7 @@ Here:
```
-
+
```csharp
var historyGenre = Builders.Filter.AnyEq(b => b.Genres, "History");
@@ -167,7 +179,19 @@ Here:
{
Console.WriteLine("Empty Collection");
}
-```
+ ```
+
+
+
+
+ ```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 c63dac4..534588e 100644
--- a/docs/40-CRUD/3-ORDER-LIMIT.mdx
+++ b/docs/40-CRUD/3-ORDER-LIMIT.mdx
@@ -99,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 c983d9b..0af14fc 100644
--- a/docs/40-CRUD/4-INSERT-DELETE.mdx
+++ b/docs/40-CRUD/4-INSERT-DELETE.mdx
@@ -135,22 +135,55 @@ 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);
+ ```
+
-
-
- ```csharp
- var newReviews = new[]
+
+
+ ```python
+ reviews = db["reviews"]
+ reviews.insert_many([
{
- 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);
- ```
-
-
+ "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",
+ }
+ ])
+ ```
+
+
@@ -176,13 +209,21 @@ DELETE FROM reviews WHERE 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["reviews"]
+ reviews.delete_many({"bookId": "0786222727"})
+ ```
diff --git a/docs/40-CRUD/5-UPDATE.mdx b/docs/40-CRUD/5-UPDATE.mdx
index f92d422..f25f45f 100644
--- a/docs/40-CRUD/5-UPDATE.mdx
+++ b/docs/40-CRUD/5-UPDATE.mdx
@@ -97,27 +97,38 @@ 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}");
- ```
-
+
+
+ ```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}");
+ ```
+
+
+
+
+ ```python
+ books = db["books"]
+ books.update_one(
+ {"title": "Treasure of the Sun"},
+ {"$set": {"pages": 449}}
+ )
+ ```
+
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..b437b48 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..0febd38 100644
--- a/docs/50-aggregation/3-sort-limit.mdx
+++ b/docs/50-aggregation/3-sort-limit.mdx
@@ -188,6 +188,56 @@ 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..98f85fe 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..369a91e 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..3788bb5 100644
--- a/docs/50-aggregation/7-merge.mdx
+++ b/docs/50-aggregation/7-merge.mdx
@@ -190,6 +190,43 @@ 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"
+ }
+ }
+ ])
+ ```
+
+
+
diff --git a/docusaurus.config.js b/docusaurus.config.js
index b85e604..3f12e20 100644
--- a/docusaurus.config.js
+++ b/docusaurus.config.js
@@ -152,7 +152,7 @@ const config = {
prism: {
theme: lightCodeTheme,
darkTheme: darkCodeTheme,
- additionalLanguages: ["powershell", "swift", "kotlin", "csharp"],
+ additionalLanguages: ["powershell", "swift", "kotlin", "python", "csharp"],
},
mermaid: {
theme: { light: "neutral", dark: "forest" },