Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 14 additions & 5 deletions docs/40-CRUD/1-WHERE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ Now, translate the following into a MongoDB query.

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

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}");
}
Expand Down Expand Up @@ -164,9 +164,9 @@ Now, translate the following into a MongoDB query.

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

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}");
}
Expand Down Expand Up @@ -203,7 +203,16 @@ Now, translate the following into a MongoDB query.
<TabItem value="csharp" label="C#">
<div>
```csharp
var booksWithGenreScienceAndMoreThan300Pages = Builders<Book>.Filter.And(Builders<Book>.Filter.AnyEq(b => b.Genres, "Science"), Builders<Book>.Filter.Gt(b => b.Pages, 300));
var booksWithGenreScienceAndMoreThan300Pages = Builders<Book>
.Filter
.And(
Builders<Book>
.Filter
.AnyEq(b => b.Genres, "Science"),
Builders<Book>
.Filter
.Gt(b => b.Pages, 300)
);

var filteredBooks = booksCollection
.Find(booksWithGenreScienceAndMoreThan300Pages)
Expand Down
8 changes: 4 additions & 4 deletions docs/40-CRUD/2-SELECT.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ Here:

var booksWithOnlyTitle = booksCollection.Find(b => true).Project<Book>(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
}
Expand Down Expand Up @@ -156,9 +156,9 @@ Here:
List<Book> sortedBooks = booksCollection.Find(historyGenre)
.Project<Book>(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
}
Expand Down
58 changes: 29 additions & 29 deletions docs/50-aggregation/2-match-project.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,22 @@ db.books.aggregate([
<TabItem value="csharp" label="C#">
<div>
```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");
}
```
</div>
</TabItem>
Expand Down Expand Up @@ -171,23 +171,23 @@ db.books.aggregate([
<TabItem value="csharp" label="C#">
<div>
```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");
}
```
</div>
</TabItem>
Expand Down
18 changes: 10 additions & 8 deletions docs/50-aggregation/3-sort-limit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,18 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
<TabItem value="addFields" label="Using $addFields">
<div>
```csharp
var pipeline = booksCollection.Aggregate()
.Match(b => b.Year > 2000)
.Match(Builders<Book>.Filter.Exists(b => b.Authors))
.AppendStage<BsonDocument>(
new BsonDocument("$addFields", new BsonDocument("numAuthors", new BsonDocument("$size", "$authors")))
var pipeline = booksCollection.Aggregate()
.Match(b => b.Year > 2000)
.Match(Builders<Book>.Filter.Exists(b => b.Authors))
.AppendStage<BsonDocument>(
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();
```
</div>
</TabItem>
Expand Down
1 change: 1 addition & 0 deletions docs/50-aggregation/4-group.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ GROUP BY year;
[BsonElement("avgRating")]
public double AvgRating { get; set; }
}

var pipeline = reviewsCollection.Aggregate()
.Group(
r => r.BookId,
Expand Down
20 changes: 18 additions & 2 deletions docs/50-aggregation/7-merge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,24 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
```csharp
var pipeline = authorsCollection.Aggregate()
.AppendStage<BsonDocument>(new BsonDocument{"$unwind", "$books"})
.AppendStage<BsonDocument>(new BsonDocument{"$group", new BsonDocument{{"_id","$name"},{"totalBooks", new BsonDocument{"$sum", 1}}}})
.AppendStage<BsonDocument>(new BsonDocument{"$merge", new BsonDocument{{"into","author_stats"},{"on","_id"},{"whenMatched","merge"},{"whenNotMatched","insert"}}});
.AppendStage<BsonDocument>(new BsonDocument{
"$group",
new BsonDocument{
{"_id","$name"},
{"totalBooks", new BsonDocument{"$sum", 1}}
}
}
)
.AppendStage<BsonDocument>(new BsonDocument{
"$merge",
new BsonDocument{
{"into","author_stats"},
{"on","_id"},
{"whenMatched","merge"},
{"whenNotMatched","insert"}
}
}
);

var result = pipeline.ToList();
```
Expand Down