From ffba5771e483d96f9abaaed9982924a5af837e98 Mon Sep 17 00:00:00 2001 From: Luce Carter Date: Fri, 26 Sep 2025 19:37:58 +0100 Subject: [PATCH 1/9] Add C# example to challenge in order-limit.mdx --- docs/40-CRUD/3-ORDER-LIMIT.mdx | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/40-CRUD/3-ORDER-LIMIT.mdx b/docs/40-CRUD/3-ORDER-LIMIT.mdx index f41011a..71bfeab 100644 --- a/docs/40-CRUD/3-ORDER-LIMIT.mdx +++ b/docs/40-CRUD/3-ORDER-LIMIT.mdx @@ -86,5 +86,18 @@ This returns the **top 10 available books** in the "Science Fiction" genre. ``` + +
+ ```csharp + var projection = Builders.Projection.Include(b => b.Title).Include(b => b.Pages); + var descendingPagesSort = Builders.Sort.Ascending("title"); + + List sortedBooksLimitedToTen = booksCollection.Find(b => true) // Empty filter to find all books + .Project(projection) + .Sort(descendingPagesSort) + .Limit(10).ToList(); + ``` +
+
From 42cc570495cac34a00a1285232d6a98550e5889c Mon Sep 17 00:00:00 2001 From: Luce Carter Date: Tue, 11 Nov 2025 15:12:23 +0000 Subject: [PATCH 2/9] Add C# examples to SELECT documentation --- docs/40-CRUD/2-SELECT.mdx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx index 349e549..48d0146 100644 --- a/docs/40-CRUD/2-SELECT.mdx +++ b/docs/40-CRUD/2-SELECT.mdx @@ -88,7 +88,7 @@ Here: await cursor.forEach((b) => { console.log(b); - }); + }); ``` @@ -100,6 +100,14 @@ Here: ``` + +
+ ```csharp + var projection = Builders.Projection.Include(b => b.Title).Exclude(b => b.Id); + booksCollection.Find(b => true).Project(projection); + ``` +
+
@@ -126,5 +134,13 @@ Here: ``` + +
+ ```csharp + var projection = Builders.Projection.Exclude(b => b.Authors).Exclude(b => b.Id); + booksCollection.Find(b => true).Project(projection); + ``` +
+
From bf2952dbdae565f8989a4a2ed2e9117f06cc8d7e Mon Sep 17 00:00:00 2001 From: Luce Carter Date: Wed, 12 Nov 2025 11:31:04 +0000 Subject: [PATCH 3/9] Added csharp as an additional language to theme for syntax highlighting --- docusaurus.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docusaurus.config.js b/docusaurus.config.js index 25f214a..b85e604 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", "csharp"], }, mermaid: { theme: { light: "neutral", dark: "forest" }, From 152ff3c9f9a3f9430107a4381a7b3fb61f8adaed Mon Sep 17 00:00:00 2001 From: Luce Carter Date: Thu, 13 Nov 2025 15:30:28 +0000 Subject: [PATCH 4/9] Added C# solution tabs to all challenge answers --- docs/40-CRUD/2-SELECT.mdx | 19 ++- docs/40-CRUD/3-ORDER-LIMIT.mdx | 4 +- docs/40-CRUD/4-INSERT-DELETE.mdx | 28 ++++- docs/40-CRUD/5-UPDATE.mdx | 17 ++- docs/50-aggregation/2-match-project.mdx | 46 +++++++ docs/50-aggregation/3-sort-limit.mdx | 148 ++++++++++++++-------- docs/50-aggregation/4-group.mdx | 158 ++++++++++++++++++------ docs/50-aggregation/5-lookup.mdx | 18 ++- docs/50-aggregation/7-merge.mdx | 101 ++++++++++----- 9 files changed, 408 insertions(+), 131 deletions(-) diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx index bd1b36a..a6d8b58 100644 --- a/docs/40-CRUD/2-SELECT.mdx +++ b/docs/40-CRUD/2-SELECT.mdx @@ -137,8 +137,23 @@ Here:
```csharp - var projection = Builders.Projection.Exclude(b => b.Authors).Exclude(b => b.Id); - booksCollection.Find(b => true).Project(projection); + var historyGenre = Builders.Filter.AnyEq(b => b.Genres, "History"); +var projection = Builders.Projection.Exclude(b => b.Id).Exclude(b => b.Authors); + +List sortedBooks = booksCollection.Find(historyGenre) + .Project(projection).ToList(); + + if(sortedBooks != null) + { + foreach(var book in sortedBooks) + { + 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 + } + } + else + { + Console.WriteLine("Empty Collection"); + } ```
diff --git a/docs/40-CRUD/3-ORDER-LIMIT.mdx b/docs/40-CRUD/3-ORDER-LIMIT.mdx index 64d29ae..09b9984 100644 --- a/docs/40-CRUD/3-ORDER-LIMIT.mdx +++ b/docs/40-CRUD/3-ORDER-LIMIT.mdx @@ -90,11 +90,11 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
```csharp var projection = Builders.Projection.Include(b => b.Title).Include(b => b.Pages); - var descendingPagesSort = Builders.Sort.Ascending("title"); + var ascendingTitleSort = Builders.Sort.Ascending("title"); List sortedBooksLimitedToTen = booksCollection.Find(b => true) // Empty filter to find all books .Project(projection) - .Sort(descendingPagesSort) + .Sort(ascendingTitleSort) .Limit(10).ToList(); ```
diff --git a/docs/40-CRUD/4-INSERT-DELETE.mdx b/docs/40-CRUD/4-INSERT-DELETE.mdx index 55bccb5..c983d9b 100644 --- a/docs/40-CRUD/4-INSERT-DELETE.mdx +++ b/docs/40-CRUD/4-INSERT-DELETE.mdx @@ -135,7 +135,22 @@ 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); + ``` +
+
@@ -159,5 +174,16 @@ DELETE FROM reviews WHERE bookId = '0786222727'; ``` + +
+ ```csharp + IMongoCollection reviewsCollection = db.GetCollection("reviews"); + + var deletionResult = reviewsCollection.DeleteMany(r => r.BookId == "0786222727"); + + Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted."); + ``` +
+
diff --git a/docs/40-CRUD/5-UPDATE.mdx b/docs/40-CRUD/5-UPDATE.mdx index bf570f5..f92d422 100644 --- a/docs/40-CRUD/5-UPDATE.mdx +++ b/docs/40-CRUD/5-UPDATE.mdx @@ -90,7 +90,7 @@ Executing the above command will insert a fresh new document in the collection,
```js await books.updateOne( - {"title": "Treasure of the Sun"}, + {"title": "Treasure of the Sun"}, {$set: {pages: 449}} ); ``` @@ -100,11 +100,24 @@ Executing the above command will insert a fresh new document in the collection,
```js db.books.updateOne( - {"title": "Treasure of the Sun"}, + {"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}"); + ``` +
+
diff --git a/docs/50-aggregation/2-match-project.mdx b/docs/50-aggregation/2-match-project.mdx index 42cd40d..a9bdd65 100644 --- a/docs/50-aggregation/2-match-project.mdx +++ b/docs/50-aggregation/2-match-project.mdx @@ -118,6 +118,29 @@ db.books.aggregate([ ```
+ +
+ ```csharp + var pipeline = booksCollection.Aggregate() + .Match(b => b.Available > 2); + + + var plentifulBooks = pipeline.ToList(); + + if(results != null) + { + foreach(var book in plentifulBooks) + { + Console.WriteLine($"Title: {book.Title} - Available: {book.Available}"); + } + } + else + { + Console.WriteLine("Empty Collection"); + } + ``` +
+
@@ -146,5 +169,28 @@ db.books.aggregate([ ``` + +
+ ```csharp + var pipeline = booksCollection.Aggregate() + .Match(b => b.Available > 2) + .Project(b => new { b.Title, b.Year }); + + var plentifulBooks = pipeline.ToList(); + + if(results != null) + { + foreach(var book in plentifulBooks) + { + Console.WriteLine($"Title: {book.Title} - Year: {book.Year}"); + } + } + else + { + Console.WriteLine("Empty Collection"); + } + ``` +
+
diff --git a/docs/50-aggregation/3-sort-limit.mdx b/docs/50-aggregation/3-sort-limit.mdx index ad2c931..3a8438d 100644 --- a/docs/50-aggregation/3-sort-limit.mdx +++ b/docs/50-aggregation/3-sort-limit.mdx @@ -77,7 +77,7 @@ SELECT * FROM books ORDER BY timestamp DESC LIMIT 5;
Answer
- There are 2 ways to solve this- + There are 2 ways to solve this- - $project - $addFields @@ -85,56 +85,106 @@ SELECT * FROM books ORDER BY timestamp DESC LIMIT 5; Learn [when to use $addFields over $project](https://www.practical-mongodb-aggregations.com/guides/project.html?highlight=%24project#when-to-use-project) ::: - - - ```js - await books.aggregate([ - { - $match: { year: { $gt: 2000 } } - }, - { - $match: { - authors: { $exists: true }, + + + + + ```js + await 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 } - }, - { - $addFields: { - numAuthors: { $size: "$authors" }, - } - }, - { - $sort: { "numAuthors": -1 } - }, - { - $limit: 1 - } - ]).toArray(); - ``` + ]).toArray(); + ``` + + + + ```js + await books.aggregate([ + { + $match: { year: { $gt: 2000 } } + }, + { + $match: { + authors: { $exists: true }, + } + }, + { + $addFields: { + numAuthors: { $size: "$authors" }, + } + }, + { + $sort: { "numAuthors": -1 } + }, + { + $limit: 1 + } + ]).toArray(); + ``` + + - - ```js - await books.aggregate([ - { - $match: { year: { $gt: 2000 } } - }, - { - $match: { - authors: { $exists: true }, - } - }, - { - $addFields: { - numAuthors: { $size: "$authors" }, - } - }, - { - $sort: { "numAuthors": -1 } - }, - { - $limit: 1 - } - ]).toArray(); - ``` + + + + +
+ ```csharp + var pipeline = booksCollection.Aggregate() + .Match(b => b.Year > 2000) + .Match(Builders.Filter.Exists(b => b.Authors)) + .Project(new BsonDocument + { + { "title", 1 }, + { "year", 1 }, + { "authors", 1 }, + { "numAuthors", new BsonDocument("$size", "$authors") } + }) + .Sort(new BsonDocument("numAuthors", -1)) + .Limit(1); + + var mostAuthors = await pipeline.ToListAsync(); + ``` +
+
+ + +
+ ```csharp + var pipeline = booksCollection.Aggregate() + .Match(b => b.Year > 2000) + .Match(Builders.Filter.Exists(b => b.Authors)) + .AppendStage( + new BsonDocument("$addFields", new BsonDocument("numAuthors", new BsonDocument("$size", "$authors"))) + ) + .Sort(new BsonDocument("numAuthors", -1)) + .Limit(1); + + var mostAuthors = pipeline.ToList(); + ``` +
+
+
diff --git a/docs/50-aggregation/4-group.mdx b/docs/50-aggregation/4-group.mdx index e3abbef..75fc922 100644 --- a/docs/50-aggregation/4-group.mdx +++ b/docs/50-aggregation/4-group.mdx @@ -133,17 +133,47 @@ GROUP BY year;
Answer - ```js - await reviews.aggregate([ - { - $group: { - _id: "$_id.bookId", - avgRating: { $avg: "$rating" } + + +
+ ```js + await reviews.aggregate([ + { + $group: { + _id: "$_id.bookId", + avgRating: { $avg: "$rating" } + } + } + ]).toArray(); + ``` +
+
+ +
+ ```csharp + public class BookRating + { + [BsonId] + public string BookId { get; set; } + + [BsonElement("avgRating")] + public double AvgRating { get; set; } } - } - ]).toArray(); - ``` - + var pipeline = reviewsCollection.Aggregate() + .Group( + r => r.BookId, + g => new BookRating + { + BookId = g.Key, + AvgRating = g.Average(r => r.Rating.Value) + } + ); + + var booksWithAvg = pipeline.ToList(); + ``` +
+
+
### 👐 2. Find users with the most number of reviews (Hint: use the `name` field in the reviews collection) @@ -151,38 +181,90 @@ GROUP BY year;
Answer
- There are 2 ways to solve this- - - $group with $sort + There are 2 ways to solve this- + - $group with $sort - $sortByCount - - - ```js - await reviews.aggregate([ - { - $group: { - _id: "$name", - totalReviews: { $sum: 1 }, - }, - }, - { - $sort: { - totalReviews: -1, - }, - }, - ]).toArray(); - ``` + + + + +
+ ```js + await reviews.aggregate([ + { + $group: { + _id: "$name", + totalReviews: { $sum: 1 }, + }, + }, + { + $sort: { + totalReviews: -1, + }, + }, + ]).toArray(); + ``` +
+
+ + +
+ ```js + await reviews.aggregate([ + { + $sortByCount: "$name", + }, + ]).toArray(); + ``` +
+
+
- - ```js - await reviews.aggregate([ - { - $sortByCount: "$name", - }, - ]).toArray(); - ``` + + + +
+ ```csharp + public class ReviewerCount + { + [BsonId] + public string Name { get; set; } + + [BsonElement("totalReviews")] + public int TotalReviews { get; set; } + } + + var pipeline = reviewsCollection.Aggregate() + .Group( + r => r.Name, + g => new ReviewerCount + { + Name = g.Key, + TotalReviews = g.Count() + } + ) + .SortByDescending(r => r.TotalReviews); + + var result = await pipeline.ToListAsync(); + ``` +
+
+ + +
+ ```csharp + var pipeline = reviewsCollection.Aggregate() + .AppendStage( + new BsonDocument("$sortByCount", "$name") + ); + + var result = pipeline.ToList(); + ``` +
+
+
-
diff --git a/docs/50-aggregation/5-lookup.mdx b/docs/50-aggregation/5-lookup.mdx index 40301ee..f50ae85 100644 --- a/docs/50-aggregation/5-lookup.mdx +++ b/docs/50-aggregation/5-lookup.mdx @@ -133,8 +133,20 @@ The $lookup operation creates an array within each book document. Using $unwind ``` - - ```js - + +
+ ```csharp + var pipeline = booksCollection.Aggregate() + .Lookup( + foreignCollection: reviewsCollection, + localField: b => b.Id, + foreignField: r => r.BookId, + @as: b => b.Reviews + ); + + var results = pipeline.ToList(); ``` +
+
+
diff --git a/docs/50-aggregation/7-merge.mdx b/docs/50-aggregation/7-merge.mdx index fa8d646..be61ae3 100644 --- a/docs/50-aggregation/7-merge.mdx +++ b/docs/50-aggregation/7-merge.mdx @@ -93,45 +93,78 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
Answer
- There are 2 ways to solve this- + There are 2 ways to solve this- - through `books` collection - through `authors` collection - - - ```js - await books.aggregate([ - { $unwind: "$authors" }, - { $group: { _id: "$authors.name", totalBooks: { $sum: 1 } } }, - { - $merge: { - into: "author_stats", - on: "_id", - whenMatched: "merge", - whenNotMatched: "insert", - }, - }, - ]).toArray(); - ``` + + + + + ```js + await books.aggregate([ + { $unwind: "$authors" }, + { $group: { _id: "$authors.name", totalBooks: { $sum: 1 } } }, + { + $merge: { + into: "author_stats", + on: "_id", + whenMatched: "merge", + whenNotMatched: "insert", + }, + }, + ]).toArray(); + ``` + + + + ```js + const authors = db.collection("authors"); + await authors.aggregate([ + { $unwind: "$books" }, + { $group: { _id: "$name", totalBooks: { $sum: 1 } } }, + { + $merge: { + into: "author_stats", + on: "_id", + whenMatched: "merge", + whenNotMatched: "insert" + } + } + ]).toArray(); + ``` + + - - ```js - const authors = db.collection("authors"); - await authors.aggregate([ - { $unwind: "$books" }, - { $group: { _id: "$name", totalBooks: { $sum: 1 } } }, - { - $merge: { - into: "author_stats", - on: "_id", - whenMatched: "merge", - whenNotMatched: "insert" - } - } - ]).toArray(); - ``` + + + +
+ ```csharp + var pipeline = booksCollection.Aggregate() + .AppendStage(new BsonDocument{"$unwind", "$authors"}) + .AppendStage(new BsonDocument{"$group", new BsonDocument{{"_id","$authors.name"},{"totalBooks", new BsonDocument{"$sum", 1}}}}) + .AppendStage(new BsonDocument{"$merge", new BsonDocument{{"into","author_stats"},{"on","_id"},{"whenMatched","merge"},{"whenNotMatched","insert"}}}); + + var result = pipeline.ToList(); + ``` +
+
+ + +
+ ```csharp + var pipeline = authorsCollection.Aggregate() + .AppendStage(new BsonDocument{"$unwind", "$books"}) + .AppendStage(new BsonDocument{"$group", new BsonDocument{{"_id","$name"},{"totalBooks", new BsonDocument{"$sum", 1}}}}) + .AppendStage(new BsonDocument{"$merge", new BsonDocument{{"into","author_stats"},{"on","_id"},{"whenMatched","merge"},{"whenNotMatched","insert"}}}); + + var result = pipeline.ToList(); + ``` +
+
+
-
From 9174e2e39dd5867fe4c2e35889cfbfa6b7ee6202 Mon Sep 17 00:00:00 2001 From: Luce Carter Date: Fri, 14 Nov 2025 11:28:55 +0000 Subject: [PATCH 5/9] SELECT - Add missing console output to help attendees see their code worked --- docs/40-CRUD/2-SELECT.mdx | 43 +++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx index a6d8b58..466364a 100644 --- a/docs/40-CRUD/2-SELECT.mdx +++ b/docs/40-CRUD/2-SELECT.mdx @@ -103,8 +103,21 @@ Here:
```csharp - var projection = Builders.Projection.Include(b => b.Title).Exclude(b => b.Id); - booksCollection.Find(b => true).Project(projection); + var projection = Builders.Projection.Include(b => b.Title).Exclude(b => b.Id); + + var booksWithOnlyTitle = booksCollection.Find(b => true).Project(projection).Limit(50).ToList(); + + if(booksWithOnlyTitle != null) + { + foreach(var book in booksWithOnlyTitle) + { + 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 + } + } + else + { + Console.WriteLine("Empty Collection"); + } ```
@@ -141,19 +154,19 @@ Here: var projection = Builders.Projection.Exclude(b => b.Id).Exclude(b => b.Authors); List sortedBooks = booksCollection.Find(historyGenre) - .Project(projection).ToList(); - - if(sortedBooks != null) - { - foreach(var book in sortedBooks) - { - 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 - } - } - else - { - Console.WriteLine("Empty Collection"); - } + .Project(projection).Limit(50).ToList(); + +if(sortedBooks != null) +{ + foreach(var book in sortedBooks) + { + 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 + } +} +else +{ + Console.WriteLine("Empty Collection"); +} ``` From 922e9407f53fe3e8f1be62d4a0fd428fdd000338 Mon Sep 17 00:00:00 2001 From: Luce Carter Date: Fri, 14 Nov 2025 11:29:30 +0000 Subject: [PATCH 6/9] match_project - Fixed incorrectly named variable in if statement --- docs/50-aggregation/2-match-project.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/50-aggregation/2-match-project.mdx b/docs/50-aggregation/2-match-project.mdx index a9bdd65..4b38e5d 100644 --- a/docs/50-aggregation/2-match-project.mdx +++ b/docs/50-aggregation/2-match-project.mdx @@ -127,7 +127,7 @@ db.books.aggregate([ var plentifulBooks = pipeline.ToList(); - if(results != null) + if(plentifulBooks != null) { foreach(var book in plentifulBooks) { @@ -178,7 +178,7 @@ db.books.aggregate([ var plentifulBooks = pipeline.ToList(); - if(results != null) + if(plentifulBooks != null) { foreach(var book in plentifulBooks) { From 1a784915de90b3bc15edd7a660aac7a2d1b67965 Mon Sep 17 00:00:00 2001 From: Luce Carter Date: Fri, 14 Nov 2025 14:30:17 +0000 Subject: [PATCH 7/9] agg_pipeline_merge - fixed bad code --- docs/50-aggregation/7-merge.mdx | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/docs/50-aggregation/7-merge.mdx b/docs/50-aggregation/7-merge.mdx index be61ae3..b3eec75 100644 --- a/docs/50-aggregation/7-merge.mdx +++ b/docs/50-aggregation/7-merge.mdx @@ -141,12 +141,21 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
```csharp - var pipeline = booksCollection.Aggregate() - .AppendStage(new BsonDocument{"$unwind", "$authors"}) - .AppendStage(new BsonDocument{"$group", new BsonDocument{{"_id","$authors.name"},{"totalBooks", new BsonDocument{"$sum", 1}}}}) - .AppendStage(new BsonDocument{"$merge", new BsonDocument{{"into","author_stats"},{"on","_id"},{"whenMatched","merge"},{"whenNotMatched","insert"}}}); - - var result = pipeline.ToList(); + var result = await booksCollection.Aggregate() + .Unwind("authors") + .Group(new BsonDocument + { + { "_id", "$authors.name" }, + { "totalBooks", new BsonDocument("$sum", 1) } + }) + .AppendStage(new BsonDocument("$merge", new BsonDocument + { + { "into", "author_stats" }, + { "on", "_id" }, + { "whenMatched", "merge" }, + { "whenNotMatched", "insert" } + })) + .ToListAsync(); ```
From 36c83154e52ca19a0a982709a220083cca00faff Mon Sep 17 00:00:00 2001 From: Luce Carter Date: Thu, 20 Nov 2025 11:47:55 +0000 Subject: [PATCH 8/9] ORDER-LIMIT - renamed variable to a better name --- docs/40-CRUD/3-ORDER-LIMIT.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/40-CRUD/3-ORDER-LIMIT.mdx b/docs/40-CRUD/3-ORDER-LIMIT.mdx index 09b9984..d69f571 100644 --- a/docs/40-CRUD/3-ORDER-LIMIT.mdx +++ b/docs/40-CRUD/3-ORDER-LIMIT.mdx @@ -92,7 +92,7 @@ This returns the **top 10 available books** in the "Science Fiction" genre. var projection = Builders.Projection.Include(b => b.Title).Include(b => b.Pages); var ascendingTitleSort = Builders.Sort.Ascending("title"); - List sortedBooksLimitedToTen = booksCollection.Find(b => true) // Empty filter to find all books + List topBooks = booksCollection.Find(b => true) // Empty filter to find all books .Project(projection) .Sort(ascendingTitleSort) .Limit(10).ToList(); From 9f429a10ae73217ee1e1dc14c172a2ea9dcee2c3 Mon Sep 17 00:00:00 2001 From: Luce Carter Date: Thu, 20 Nov 2025 11:48:09 +0000 Subject: [PATCH 9/9] Fixed indentation of some C# code snippets --- docs/40-CRUD/2-SELECT.mdx | 36 ++++++++-------- docs/40-CRUD/3-ORDER-LIMIT.mdx | 6 +-- docs/50-aggregation/2-match-project.mdx | 57 ++++++++++++------------- docs/50-aggregation/3-sort-limit.mdx | 46 ++++++++++---------- docs/50-aggregation/5-lookup.mdx | 12 +++--- 5 files changed, 78 insertions(+), 79 deletions(-) diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx index 466364a..c380ea0 100644 --- a/docs/40-CRUD/2-SELECT.mdx +++ b/docs/40-CRUD/2-SELECT.mdx @@ -150,24 +150,24 @@ Here:
```csharp - var historyGenre = Builders.Filter.AnyEq(b => b.Genres, "History"); -var projection = Builders.Projection.Exclude(b => b.Id).Exclude(b => b.Authors); - -List sortedBooks = booksCollection.Find(historyGenre) - .Project(projection).Limit(50).ToList(); - -if(sortedBooks != null) -{ - foreach(var book in sortedBooks) - { - 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 - } -} -else -{ - Console.WriteLine("Empty Collection"); -} - ``` + var historyGenre = Builders.Filter.AnyEq(b => b.Genres, "History"); + var projection = Builders.Projection.Exclude(b => b.Id).Exclude(b => b.Authors); + + List sortedBooks = booksCollection.Find(historyGenre) + .Project(projection).Limit(50).ToList(); + + if(sortedBooks != null) + { + foreach(var book in sortedBooks) + { + 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 + } + } + else + { + Console.WriteLine("Empty Collection"); + } +```
diff --git a/docs/40-CRUD/3-ORDER-LIMIT.mdx b/docs/40-CRUD/3-ORDER-LIMIT.mdx index d69f571..c63dac4 100644 --- a/docs/40-CRUD/3-ORDER-LIMIT.mdx +++ b/docs/40-CRUD/3-ORDER-LIMIT.mdx @@ -93,9 +93,9 @@ This returns the **top 10 available books** in the "Science Fiction" genre. var ascendingTitleSort = Builders.Sort.Ascending("title"); List topBooks = booksCollection.Find(b => true) // Empty filter to find all books - .Project(projection) - .Sort(ascendingTitleSort) - .Limit(10).ToList(); + .Project(projection) + .Sort(ascendingTitleSort) + .Limit(10).ToList(); ``` diff --git a/docs/50-aggregation/2-match-project.mdx b/docs/50-aggregation/2-match-project.mdx index 4b38e5d..9c6006c 100644 --- a/docs/50-aggregation/2-match-project.mdx +++ b/docs/50-aggregation/2-match-project.mdx @@ -121,23 +121,22 @@ db.books.aggregate([
```csharp - var pipeline = booksCollection.Aggregate() - .Match(b => b.Available > 2); - - - var plentifulBooks = pipeline.ToList(); - - if(plentifulBooks != null) - { - foreach(var book in plentifulBooks) - { - Console.WriteLine($"Title: {book.Title} - Available: {book.Available}"); - } - } - else - { - Console.WriteLine("Empty Collection"); - } + var pipeline = booksCollection.Aggregate() + .Match(b => b.Available > 2); + + var plentifulBooks = pipeline.ToList(); + + if(plentifulBooks != null) + { + foreach(var book in plentifulBooks) + { + Console.WriteLine($"Title: {book.Title} - Available: {book.Available}"); + } + } + else + { + Console.WriteLine("Empty Collection"); + } ```
@@ -172,23 +171,23 @@ db.books.aggregate([
```csharp - var pipeline = booksCollection.Aggregate() - .Match(b => b.Available > 2) - .Project(b => new { b.Title, b.Year }); + var pipeline = booksCollection.Aggregate() + .Match(b => b.Available > 2) + .Project(b => new { b.Title, b.Year }); - var plentifulBooks = pipeline.ToList(); + var plentifulBooks = pipeline.ToList(); - if(plentifulBooks != null) - { - foreach(var book in plentifulBooks) + if(plentifulBooks != null) { + foreach(var book in plentifulBooks) + { Console.WriteLine($"Title: {book.Title} - Year: {book.Year}"); + } + } + else + { + Console.WriteLine("Empty Collection"); } - } - else - { - Console.WriteLine("Empty Collection"); - } ```
diff --git a/docs/50-aggregation/3-sort-limit.mdx b/docs/50-aggregation/3-sort-limit.mdx index 3a8438d..df71d81 100644 --- a/docs/50-aggregation/3-sort-limit.mdx +++ b/docs/50-aggregation/3-sort-limit.mdx @@ -151,19 +151,19 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
```csharp var pipeline = booksCollection.Aggregate() - .Match(b => b.Year > 2000) - .Match(Builders.Filter.Exists(b => b.Authors)) - .Project(new BsonDocument - { - { "title", 1 }, - { "year", 1 }, - { "authors", 1 }, - { "numAuthors", new BsonDocument("$size", "$authors") } - }) - .Sort(new BsonDocument("numAuthors", -1)) - .Limit(1); - - var mostAuthors = await pipeline.ToListAsync(); + .Match(b => b.Year > 2000) + .Match(Builders.Filter.Exists(b => b.Authors)) + .Project(new BsonDocument + { + { "title", 1 }, + { "year", 1 }, + { "authors", 1 }, + { "numAuthors", new BsonDocument("$size", "$authors") } + }) + .Sort(new BsonDocument("numAuthors", -1)) + .Limit(1); + + var mostAuthors = await pipeline.ToListAsync(); ```
@@ -171,16 +171,16 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
```csharp - var pipeline = booksCollection.Aggregate() - .Match(b => b.Year > 2000) - .Match(Builders.Filter.Exists(b => b.Authors)) - .AppendStage( - new BsonDocument("$addFields", new BsonDocument("numAuthors", new BsonDocument("$size", "$authors"))) - ) - .Sort(new BsonDocument("numAuthors", -1)) - .Limit(1); - - var mostAuthors = pipeline.ToList(); + var pipeline = booksCollection.Aggregate() + .Match(b => b.Year > 2000) + .Match(Builders.Filter.Exists(b => b.Authors)) + .AppendStage( + new BsonDocument("$addFields", new BsonDocument("numAuthors", new BsonDocument("$size", "$authors"))) + ) + .Sort(new BsonDocument("numAuthors", -1)) + .Limit(1); + + var mostAuthors = pipeline.ToList(); ```
diff --git a/docs/50-aggregation/5-lookup.mdx b/docs/50-aggregation/5-lookup.mdx index f50ae85..fe81147 100644 --- a/docs/50-aggregation/5-lookup.mdx +++ b/docs/50-aggregation/5-lookup.mdx @@ -137,12 +137,12 @@ The $lookup operation creates an array within each book document. Using $unwind
```csharp var pipeline = booksCollection.Aggregate() - .Lookup( - foreignCollection: reviewsCollection, - localField: b => b.Id, - foreignField: r => r.BookId, - @as: b => b.Reviews - ); + .Lookup( + foreignCollection: reviewsCollection, + localField: b => b.Id, + foreignField: r => r.BookId, + @as: b => b.Reviews + ); var results = pipeline.ToList(); ```