Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/40-CRUD/1-WHERE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ Now, translate the following into a MongoDB query.
```
</div>
</TabItem>
<TabItem value="python" label="Python">
<div>
```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']}")
```
</div>
</TabItem>
</Tabs>

</details>
Expand Down Expand Up @@ -174,6 +184,16 @@ Now, translate the following into a MongoDB query.
```
</div>
</TabItem>
<TabItem value="python" label="Python">
<div>
```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']}")
```
</div>
</TabItem>
</Tabs>
</details>

Expand Down Expand Up @@ -225,5 +245,15 @@ Now, translate the following into a MongoDB query.
```
</div>
</TabItem>
<TabItem value="python" label="Python">
<div>
```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']}")
```
</div>
</TabItem>
</Tabs>
</details>
24 changes: 22 additions & 2 deletions docs/40-CRUD/2-SELECT.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Here:
```
</div>
</TabItem>
<TabItem value="csharp" label="C#">
<TabItem value="csharp" label="C#">
<div>
```csharp
var projection = Builders<Book>.Projection.Include(b => b.Title).Exclude(b => b.Id);
Expand All @@ -121,6 +121,16 @@ Here:
```
</div>
</TabItem>
<TabItem value="python" label="Python">
<div>
```python
books_with_title_only = books.find({}, {"title": 1, "_id": 0}).limit(10)

for book in books_with_title_only:
print(book)
```
</div>
</TabItem>
</Tabs>
</details>

Expand All @@ -147,7 +157,7 @@ Here:
```
</div>
</TabItem>
<TabItem value="csharp" label="C#">
<TabItem value="csharp" label="C#">
<div>
```csharp
var historyGenre = Builders<Book>.Filter.AnyEq(b => b.Genres, "History");
Expand All @@ -170,5 +180,15 @@ Here:
```
</div>
</TabItem>
<TabItem value="python" label="Python">
<div>
```python
books_with_genre_history = books.find({"genres": "History"}, {"_id": 0, "authors": 0}).limit(10)

for book in books_with_genre_history:
print(book)
```
</div>
</TabItem>
</Tabs>
</details>
10 changes: 10 additions & 0 deletions docs/40-CRUD/3-ORDER-LIMIT.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,15 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
```
</div>
</TabItem>
<TabItem value="python" label="Python">
<div>
```python
books_sorted_by_title = books.find({}).sort("title", 1).limit(10)

for book in books_sorted_by_title:
print(book)
```
</div>
</TabItem>
</Tabs>
</details>
81 changes: 61 additions & 20 deletions docs/40-CRUD/4-INSERT-DELETE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,55 @@ DELETE FROM reviews WHERE bookId = '0786222727';
]);
```
</div>
</TabItem>
<TabItem value="csharp" label="C#">
<div>
```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);
```
</div>
</TabItem>
<TabItem value="csharp" label="C#">
<div>
```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);
```
</div>
</TabItem>
<TabItem value="python" label="Python">
<div>
```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",
}
])
```
</div>
</TabItem>
</Tabs>
</details>

Expand All @@ -176,13 +209,21 @@ DELETE FROM reviews WHERE bookId = '0786222727';
</TabItem>
<TabItem value="csharp" label="C#">
<div>
```csharp
IMongoCollection<Review> reviewsCollection = db.GetCollection<Review>("reviews");
```csharp
IMongoCollection<Review> reviewsCollection = db.GetCollection<Review>("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.");
```
</div>
</TabItem>
<TabItem value="python" label="Python">
<div>
```python
reviews = db.collection("reviews")
reviews.delete_many({"bookId": "0786222727"})
```
</div>
</TabItem>
</Tabs>
Expand Down
51 changes: 31 additions & 20 deletions docs/40-CRUD/5-UPDATE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,38 @@ Executing the above command will insert a fresh new document in the collection,
</div>
</TabItem>
<TabItem value="mongosh" label="mongosh">
<div>
```js
db.books.updateOne(
{"title": "Treasure of the Sun"},
{$set: {pages: 449}}
);
```
</div>
<div>
```js
db.books.updateOne(
{"title": "Treasure of the Sun"},
{$set: {pages: 449}}
);
```
</div>
</TabItem>
<TabItem value="csharp" label="C#">
<div>
```csharp
var filter = Builders<Book>.Filter.Eq(b => b.Title, "Treasure of the Sun");
var update = Builders<Book>.Update.Set(b => b.Pages, 449);

var result = booksCollection.UpdateOne(filter, update);

// Optionally inspect the outcome
Console.WriteLine($"Matched: {result.MatchedCount}, Modified: {result.ModifiedCount}");
```
</div>
<TabItem value="csharp" label="C#">
<div>
```csharp
var filter = Builders<Book>.Filter.Eq(b => b.Title, "Treasure of the Sun");
var update = Builders<Book>.Update.Set(b => b.Pages, 449);

var result = booksCollection.UpdateOne(filter, update);

// Optionally inspect the outcome
Console.WriteLine($"Matched: {result.MatchedCount}, Modified: {result.ModifiedCount}");
```
</div>
</TabItem>
<TabItem value="python" label="Python">
<div>
```python
reviews = db.collection("books")
reviews.update_one(
{"title": "Treasure of the Sun"},
{"$set": {"pages": 449}}
)
```
</div>
</TabItem>
</Tabs>
</details>
8 changes: 8 additions & 0 deletions docs/50-aggregation/1-aggregation-intro.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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`. 🚀
25 changes: 25 additions & 0 deletions docs/50-aggregation/2-match-project.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ db.books.aggregate([
```
</div>
</TabItem>
<TabItem value="python" label="Python">
<div>
```python
books_two_copies = books.aggregate([
{ "$match": { "available": { "$gt": 2 } } }
])

for book in books_two_copies:
print(book)
```
</div>
</TabItem>
</Tabs>
</details>

Expand Down Expand Up @@ -191,5 +203,18 @@ db.books.aggregate([
```
</div>
</TabItem>
<TabItem value="python" label="Python">
<div>
```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']}")
```
</div>
</TabItem>
</Tabs>
</details>
48 changes: 48 additions & 0 deletions docs/50-aggregation/3-sort-limit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,54 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
</TabItem>
</Tabs>
</TabItem>
<TabItem value="python" label="Python">
<Tabs groupId="aggregations-python" defaultValue="addFields">
<TabItem value="project" label="Using $project">
<div>
```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)
```
</div>
</TabItem>

<TabItem value="addFields" label="Using $addFields">
<div>
```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)
```
</div>
</TabItem>
</Tabs>
</TabItem>
</Tabs>
</div>
</details>
Loading