Skip to content

Commit d156c4a

Browse files
feat: Merge Python feature
2 parents bafe066 + 7d02a3d commit d156c4a

File tree

12 files changed

+405
-80
lines changed

12 files changed

+405
-80
lines changed

docs/40-CRUD/1-WHERE.mdx

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,20 @@ Now, translate the following into a MongoDB query.
129129
}
130130
```
131131
</div>
132-
</TabItem><TabItem value="Java" label="Java">
132+
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>
145+
</TabItem><TabItem value="Java" label="Java">
133146
<div>
134147
```Java
135148
Bson filter = eq("totalInventory", 5);
@@ -192,7 +205,18 @@ Now, translate the following into a MongoDB query.
192205
}
193206
```
194207
</div>
195-
</TabItem><TabItem value="Java" label="Java">
208+
</TabItem>
209+
<TabItem value="python" label="Python">
210+
<div>
211+
```python
212+
books_with_more_than_300_pages = books.find({"pages": {"$gt": 300}})
213+
214+
for book in books_with_more_than_300_pages:
215+
print(f"Book Title: {book['title']} - Pages: {book['pages']}")
216+
```
217+
</div>
218+
</TabItem>
219+
<TabItem value="Java" label="Java">
196220
<div>
197221
```Java
198222
Bson filter = gt("pages", 300);
@@ -262,7 +286,18 @@ Now, translate the following into a MongoDB query.
262286
}
263287
```
264288
</div>
265-
</TabItem><TabItem value="Java" label="Java">
289+
</TabItem>
290+
<TabItem value="python" label="Python">
291+
<div>
292+
```python
293+
books_with_genre_science_and_more_than_300_pages = books.find({"genres": "Science", "pages": {"$gt": 300}})
294+
295+
for book in books_with_genre_science_and_more_than_300_pages:
296+
print(f"Book Title: {book['title']} - Pages: {book['pages']}")
297+
```
298+
</div>
299+
</TabItem>
300+
<TabItem value="Java" label="Java">
266301
<div>
267302
```Java
268303
Bson filter = and(

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: 93 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -135,46 +135,80 @@ 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><TabItem value="Java" label="Java">
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>
187+
<TabItem value="Java" label="Java">
154188
<div>
155189
```Java
156-
var reviews = library.getCollection("reviews");
157-
158-
reviews.insertMany(List.of(
159-
new Document("text", "Thrilling end.")
160-
.append("rating", 4)
161-
.append("name", "Mark")
162-
.append("bookId", "0786222727"),
163-
164-
new Document("text", "Very expensive")
165-
.append("rating", 3)
166-
.append("name", "Yun")
167-
.append("bookId", "0786222727"),
168-
169-
new Document("text", "Must read!.")
170-
.append("rating", 6)
171-
.append("name", "Raj")
172-
.append("bookId", "0786222727"),
173-
174-
new Document("text", "Extremely satisfied with the storyline!")
175-
.append("rating", 5)
176-
.append("name", "Lisa")
177-
.append("bookId", "0786222727")));
190+
var reviews = library.getCollection("reviews");
191+
192+
reviews.insertMany(List.of(
193+
new Document("text", "Thrilling end.")
194+
.append("rating", 4)
195+
.append("name", "Mark")
196+
.append("bookId", "0786222727"),
197+
198+
new Document("text", "Very expensive")
199+
.append("rating", 3)
200+
.append("name", "Yun")
201+
.append("bookId", "0786222727"),
202+
203+
new Document("text", "Must read!.")
204+
.append("rating", 6)
205+
.append("name", "Raj")
206+
.append("bookId", "0786222727"),
207+
208+
new Document("text", "Extremely satisfied with the storyline!")
209+
.append("rating", 5)
210+
.append("name", "Lisa")
211+
.append("bookId", "0786222727")));
178212
```
179213
</div>
180214
</TabItem>
@@ -203,28 +237,37 @@ DELETE FROM reviews WHERE bookId = '0786222727';
203237
</TabItem>
204238
<TabItem value="csharp" label="C#">
205239
<div>
206-
```csharp
207-
IMongoCollection<Review> reviewsCollection = db.GetCollection<Review>("reviews");
240+
```csharp
241+
IMongoCollection<Review> reviewsCollection = db.GetCollection<Review>("reviews");
208242

209-
var deletionResult = reviewsCollection.DeleteMany(r => r.BookId == "0786222727");
243+
var deletionResult = reviewsCollection.DeleteMany(r => r.BookId == "0786222727");
210244

211-
Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted.");
212-
```
245+
Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted.");
246+
```
213247
</div>
214-
</TabItem><TabItem value="Java" label="Java">
248+
</TabItem>
249+
<TabItem value="python" label="Python">
215250
<div>
216-
```Java
217-
import static com.mongodb.client.model.Filters.eq;
218-
import org.bson.conversions.Bson;
251+
```python
252+
reviews = db["reviews"]
253+
reviews.delete_many({"bookId": "0786222727"})
254+
```
255+
</div>
256+
</TabItem>
257+
<TabItem value="Java" label="Java">
258+
<div>
259+
```Java
260+
import static com.mongodb.client.model.Filters.eq;
261+
import org.bson.conversions.Bson;
219262

220-
MongoCollection<Document> reviews = library.getCollection("reviews");
263+
MongoCollection<Document> reviews = library.getCollection("reviews");
221264

222-
Bson filter = eq("bookId", "0786222727");
265+
Bson filter = eq("bookId", "0786222727");
223266

224-
DeleteResult result = reviews.deleteMany(filter);
225-
System.out.println(result.getDeletedCount() + " reviews deleted.");
267+
DeleteResult result = reviews.deleteMany(filter);
268+
System.out.println(result.getDeletedCount() + " reviews deleted.");
226269
```
227270
</div>
228271
</TabItem>
229272
</Tabs>
230-
</details>
273+
</details>

docs/40-CRUD/5-UPDATE.mdx

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,38 +97,51 @@ 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>
108+
</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>
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>
108132
</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>
121-
</TabItem><TabItem value="Java" label="Java">
133+
<TabItem value="Java" label="Java">
122134
<div>
123-
```Java
135+
```Java
124136
import static com.mongodb.client.model.Filters.eq;
125137

126138
var query = eq("title", "Treasure of the Sun");
127139
var update = Updates.set("pages", 449);
140+
128141
UpdateResult updateResult = books.updateOne(query, update);
129142
System.out.println("Modified document count: " + updateResult.getModifiedCount());
130143
```
131144
</div>
132145
</TabItem>
133146
</Tabs>
134-
</details>
147+
</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`. 🚀

0 commit comments

Comments
 (0)