From 44075729188103c4c20868e55682766eda95ab16 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Mon, 24 Nov 2025 16:03:47 +0200 Subject: [PATCH] Make a couple of indentation changes for readability --- docs/40-CRUD/1-WHERE.mdx | 19 +++++--- docs/40-CRUD/2-SELECT.mdx | 8 ++-- docs/50-aggregation/2-match-project.mdx | 58 ++++++++++++------------- docs/50-aggregation/3-sort-limit.mdx | 18 ++++---- docs/50-aggregation/4-group.mdx | 1 + docs/50-aggregation/7-merge.mdx | 20 ++++++++- 6 files changed, 76 insertions(+), 48 deletions(-) diff --git a/docs/40-CRUD/1-WHERE.mdx b/docs/40-CRUD/1-WHERE.mdx index c798cc7..751bee8 100644 --- a/docs/40-CRUD/1-WHERE.mdx +++ b/docs/40-CRUD/1-WHERE.mdx @@ -120,9 +120,9 @@ Now, translate the following into a MongoDB query. List filteredBooks = booksCollection.Find(booksWithTotalInventoryOf5).ToList(); - if(filteredBooks != null) + if (filteredBooks != null) { - foreach(var book in filteredBooks) + foreach (var book in filteredBooks) { Console.WriteLine($"Book Title: {book.Title} - Total Inventory: {book.TotalInventory}"); } @@ -164,9 +164,9 @@ Now, translate the following into a MongoDB query. List filteredBooks = booksCollection.Find(booksWithMoreThan300Pages).ToList(); - if(filteredBooks != null) + if (filteredBooks != null) { - foreach(var book in filteredBooks) + foreach (var book in filteredBooks) { Console.WriteLine($"Book Title: {book.Title} - Pages: {book.Pages}"); } @@ -203,7 +203,16 @@ Now, translate the following into a MongoDB query.
```csharp - var booksWithGenreScienceAndMoreThan300Pages = Builders.Filter.And(Builders.Filter.AnyEq(b => b.Genres, "Science"), Builders.Filter.Gt(b => b.Pages, 300)); + var booksWithGenreScienceAndMoreThan300Pages = Builders + .Filter + .And( + Builders + .Filter + .AnyEq(b => b.Genres, "Science"), + Builders + .Filter + .Gt(b => b.Pages, 300) + ); var filteredBooks = booksCollection .Find(booksWithGenreScienceAndMoreThan300Pages) diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx index c380ea0..c80fcd3 100644 --- a/docs/40-CRUD/2-SELECT.mdx +++ b/docs/40-CRUD/2-SELECT.mdx @@ -107,9 +107,9 @@ Here: var booksWithOnlyTitle = booksCollection.Find(b => true).Project(projection).Limit(50).ToList(); - if(booksWithOnlyTitle != null) + if (booksWithOnlyTitle != null) { - foreach(var book in booksWithOnlyTitle) + 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 } @@ -156,9 +156,9 @@ Here: List sortedBooks = booksCollection.Find(historyGenre) .Project(projection).Limit(50).ToList(); - if(sortedBooks != null) + if (sortedBooks != null) { - foreach(var book in sortedBooks) + 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 } diff --git a/docs/50-aggregation/2-match-project.mdx b/docs/50-aggregation/2-match-project.mdx index 9c6006c..9c1915a 100644 --- a/docs/50-aggregation/2-match-project.mdx +++ b/docs/50-aggregation/2-match-project.mdx @@ -121,22 +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"); + } ```
@@ -171,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) - { - Console.WriteLine($"Title: {book.Title} - Year: {book.Year}"); - } - } - else + if (plentifulBooks != null) + { + foreach (var book in plentifulBooks) { - Console.WriteLine("Empty Collection"); + 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 df71d81..b30716b 100644 --- a/docs/50-aggregation/3-sort-limit.mdx +++ b/docs/50-aggregation/3-sort-limit.mdx @@ -171,16 +171,18 @@ 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"))) + 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); + .Sort(new BsonDocument("numAuthors", -1)) + .Limit(1); - var mostAuthors = pipeline.ToList(); + var mostAuthors = pipeline.ToList(); ```
diff --git a/docs/50-aggregation/4-group.mdx b/docs/50-aggregation/4-group.mdx index 75fc922..bb30596 100644 --- a/docs/50-aggregation/4-group.mdx +++ b/docs/50-aggregation/4-group.mdx @@ -159,6 +159,7 @@ GROUP BY year; [BsonElement("avgRating")] public double AvgRating { get; set; } } + var pipeline = reviewsCollection.Aggregate() .Group( r => r.BookId, diff --git a/docs/50-aggregation/7-merge.mdx b/docs/50-aggregation/7-merge.mdx index b3eec75..cc0d793 100644 --- a/docs/50-aggregation/7-merge.mdx +++ b/docs/50-aggregation/7-merge.mdx @@ -165,8 +165,24 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks); ```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"}}}); + .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(); ```