Skip to content

Commit 8f86f38

Browse files
Merge branch 'main' into add-python-solutions
2 parents 5301051 + 7bf73fd commit 8f86f38

File tree

11 files changed

+484
-135
lines changed

11 files changed

+484
-135
lines changed

docs/40-CRUD/1-WHERE.mdx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ Now, translate the following into a MongoDB query.
120120

121121
List<Book> filteredBooks = booksCollection.Find(booksWithTotalInventoryOf5).ToList<Book>();
122122

123-
if(filteredBooks != null)
123+
if (filteredBooks != null)
124124
{
125-
foreach(var book in filteredBooks)
125+
foreach (var book in filteredBooks)
126126
{
127127
Console.WriteLine($"Book Title: {book.Title} - Total Inventory: {book.TotalInventory}");
128128
}
@@ -174,9 +174,9 @@ Now, translate the following into a MongoDB query.
174174

175175
List<Book> filteredBooks = booksCollection.Find(booksWithMoreThan300Pages).ToList<Book>();
176176

177-
if(filteredBooks != null)
177+
if (filteredBooks != null)
178178
{
179-
foreach(var book in filteredBooks)
179+
foreach (var book in filteredBooks)
180180
{
181181
Console.WriteLine($"Book Title: {book.Title} - Pages: {book.Pages}");
182182
}
@@ -223,7 +223,16 @@ Now, translate the following into a MongoDB query.
223223
<TabItem value="csharp" label="C#">
224224
<div>
225225
```csharp
226-
var booksWithGenreScienceAndMoreThan300Pages = Builders<Book>.Filter.And(Builders<Book>.Filter.AnyEq(b => b.Genres, "Science"), Builders<Book>.Filter.Gt(b => b.Pages, 300));
226+
var booksWithGenreScienceAndMoreThan300Pages = Builders<Book>
227+
.Filter
228+
.And(
229+
Builders<Book>
230+
.Filter
231+
.AnyEq(b => b.Genres, "Science"),
232+
Builders<Book>
233+
.Filter
234+
.Gt(b => b.Pages, 300)
235+
);
227236

228237
var filteredBooks = booksCollection
229238
.Find(booksWithGenreScienceAndMoreThan300Pages)

docs/40-CRUD/2-SELECT.mdx

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Here:
8888

8989
await cursor.forEach((b) => {
9090
console.log(b);
91-
});
91+
});
9292
```
9393
</div>
9494

@@ -110,6 +110,26 @@ Here:
110110
```
111111
</div>
112112

113+
<TabItem value="csharp" label="C#">
114+
<div>
115+
```csharp
116+
var projection = Builders<Book>.Projection.Include(b => b.Title).Exclude(b => b.Id);
117+
118+
var booksWithOnlyTitle = booksCollection.Find(b => true).Project<Book>(projection).Limit(50).ToList();
119+
120+
if (booksWithOnlyTitle != null)
121+
{
122+
foreach (var book in booksWithOnlyTitle)
123+
{
124+
Console.WriteLine(book.ToJson()); // Shows the entire BSON document as JSON to show that some fields are null because they are not returned due to the projection
125+
}
126+
}
127+
else
128+
{
129+
Console.WriteLine("Empty Collection");
130+
}
131+
```
132+
</div>
113133
</TabItem>
114134
</Tabs>
115135
</details>
@@ -146,6 +166,28 @@ Here:
146166
print(book)
147167
```
148168
</div>
169+
<TabItem value="csharp" label="C#">
170+
<div>
171+
```csharp
172+
var historyGenre = Builders<Book>.Filter.AnyEq(b => b.Genres, "History");
173+
var projection = Builders<Book>.Projection.Exclude(b => b.Id).Exclude(b => b.Authors);
174+
175+
List<Book> sortedBooks = booksCollection.Find(historyGenre)
176+
.Project<Book>(projection).Limit(50).ToList();
177+
178+
if (sortedBooks != null)
179+
{
180+
foreach (var book in sortedBooks)
181+
{
182+
Console.WriteLine(book.ToJson()); // Shows the entire BSON document as JSON to show that some fields are null because they are not returned due to the projection
183+
}
184+
}
185+
else
186+
{
187+
Console.WriteLine("Empty Collection");
188+
}
189+
```
190+
</div>
149191
</TabItem>
150192
</Tabs>
151193
</details>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
9595
print(book)
9696
```
9797
</div>
98+
<TabItem value="csharp" label="C#">
99+
<div>
100+
```csharp
101+
var projection = Builders<Book>.Projection.Include(b => b.Title).Include(b => b.Pages);
102+
var ascendingTitleSort = Builders<Book>.Sort.Ascending("title");
103+
104+
List<Book> topBooks = booksCollection.Find(b => true) // Empty filter to find all books
105+
.Project<Book>(projection)
106+
.Sort(ascendingTitleSort)
107+
.Limit(10).ToList();
108+
```
109+
</div>
98110
</TabItem>
99111
</Tabs>
100112
</details>

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ DELETE FROM reviews WHERE bookId = '0786222727';
169169
```
170170
</div>
171171
</TabItem>
172+
</TabItem>
173+
<TabItem value="csharp" label="C#">
174+
<div>
175+
```csharp
176+
var newReviews = new[]
177+
{
178+
new Review { Text = "Thrilling end.", Rating = 4, Name = "Mark", BookId = "0786222727" },
179+
new Review { Text = "Must read!", Rating = 5, Name = "Raj", BookId = "0786222727" },
180+
new Review { Text = "Very expensive", Rating = 3, Name = "Yun", BookId = "0786222727" },
181+
new Review { Text = "Extremely satisfied with the storyline!", Rating = 5, Name = "Lisa", BookId = "0786222727" }
182+
};
183+
184+
reviewsCollection.InsertMany(newReviews);
185+
```
186+
</div>
187+
</TabItem>
172188
</Tabs>
173189
</details>
174190

@@ -199,6 +215,16 @@ DELETE FROM reviews WHERE bookId = '0786222727';
199215
reviews.delete_many({"bookId": "0786222727"})
200216
```
201217
</div>
218+
<TabItem value="csharp" label="C#">
219+
<div>
220+
```csharp
221+
IMongoCollection<Review> reviewsCollection = db.GetCollection<Review>("reviews");
222+
223+
var deletionResult = reviewsCollection.DeleteMany(r => r.BookId == "0786222727");
224+
225+
Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted.");
226+
```
227+
</div>
202228
</TabItem>
203229
</Tabs>
204230
</details>

docs/40-CRUD/5-UPDATE.mdx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Executing the above command will insert a fresh new document in the collection,
9090
<div>
9191
```js
9292
await books.updateOne(
93-
{"title": "Treasure of the Sun"},
93+
{"title": "Treasure of the Sun"},
9494
{$set: {pages: 449}}
9595
);
9696
```
@@ -100,7 +100,7 @@ Executing the above command will insert a fresh new document in the collection,
100100
<div>
101101
```js
102102
db.books.updateOne(
103-
{"title": "Treasure of the Sun"},
103+
{"title": "Treasure of the Sun"},
104104
{$set: {pages: 449}}
105105
);
106106
```
@@ -116,6 +116,18 @@ Executing the above command will insert a fresh new document in the collection,
116116
)
117117
```
118118
</div>
119+
<TabItem value="csharp" label="C#">
120+
<div>
121+
```csharp
122+
var filter = Builders<Book>.Filter.Eq(b => b.Title, "Treasure of the Sun");
123+
var update = Builders<Book>.Update.Set(b => b.Pages, 449);
124+
125+
var result = booksCollection.UpdateOne(filter, update);
126+
127+
// Optionally inspect the outcome
128+
Console.WriteLine($"Matched: {result.MatchedCount}, Modified: {result.ModifiedCount}");
129+
```
130+
</div>
119131
</TabItem>
120132
</Tabs>
121133
</details>

docs/50-aggregation/2-match-project.mdx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ db.books.aggregate([
118118
```
119119
</div>
120120
</TabItem>
121+
<TabItem value="csharp" label="C#">
122+
<div>
123+
```csharp
124+
var pipeline = booksCollection.Aggregate()
125+
.Match(b => b.Available > 2);
126+
127+
var plentifulBooks = pipeline.ToList();
128+
129+
if (plentifulBooks != null)
130+
{
131+
foreach (var book in plentifulBooks)
132+
{
133+
Console.WriteLine($"Title: {book.Title} - Available: {book.Available}");
134+
}
135+
}
136+
else
137+
{
138+
Console.WriteLine("Empty Collection");
139+
}
140+
```
141+
</div>
142+
</TabItem>
121143
</Tabs>
122144
</details>
123145

@@ -146,5 +168,28 @@ db.books.aggregate([
146168
```
147169
</div>
148170
</TabItem>
171+
<TabItem value="csharp" label="C#">
172+
<div>
173+
```csharp
174+
var pipeline = booksCollection.Aggregate()
175+
.Match(b => b.Available > 2)
176+
.Project(b => new { b.Title, b.Year });
177+
178+
var plentifulBooks = pipeline.ToList();
179+
180+
if (plentifulBooks != null)
181+
{
182+
foreach (var book in plentifulBooks)
183+
{
184+
Console.WriteLine($"Title: {book.Title} - Year: {book.Year}");
185+
}
186+
}
187+
else
188+
{
189+
Console.WriteLine("Empty Collection");
190+
}
191+
```
192+
</div>
193+
</TabItem>
149194
</Tabs>
150195
</details>

0 commit comments

Comments
 (0)