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
46 changes: 45 additions & 1 deletion docs/40-CRUD/2-SELECT.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Here:

await cursor.forEach((b) => {
console.log(b);
});
});
```
</div>

Expand All @@ -100,6 +100,27 @@ Here:
```
</div>
</TabItem>
<TabItem value="csharp" label="C#">
<div>
```csharp
var projection = Builders<Book>.Projection.Include(b => b.Title).Exclude(b => b.Id);

var booksWithOnlyTitle = booksCollection.Find(b => true).Project<Book>(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");
}
```
</div>
</TabItem>
</Tabs>
</details>

Expand All @@ -126,5 +147,28 @@ Here:
```
</div>
</TabItem>
<TabItem value="csharp" label="C#">
<div>
```csharp
var historyGenre = Builders<Book>.Filter.AnyEq(b => b.Genres, "History");
var projection = Builders<Book>.Projection.Exclude(b => b.Id).Exclude(b => b.Authors);

List<Book> sortedBooks = booksCollection.Find(historyGenre)
.Project<Book>(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");
}
```
</div>
</TabItem>
</Tabs>
</details>
13 changes: 13 additions & 0 deletions docs/40-CRUD/3-ORDER-LIMIT.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,18 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
```
</div>
</TabItem>
<TabItem value="csharp" label="C#">
<div>
```csharp
var projection = Builders<Book>.Projection.Include(b => b.Title).Include(b => b.Pages);
var ascendingTitleSort = Builders<Book>.Sort.Ascending("title");

List<Book> topBooks = booksCollection.Find(b => true) // Empty filter to find all books
.Project<Book>(projection)
.Sort(ascendingTitleSort)
.Limit(10).ToList();
```
</div>
</TabItem>
</Tabs>
</details>
28 changes: 27 additions & 1 deletion docs/40-CRUD/4-INSERT-DELETE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,22 @@ DELETE FROM reviews WHERE bookId = '0786222727';
]);
```
</div>
</TabItem>
</TabItem>
<TabItem value="csharp" label="C#">
<div>
```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);
```
</div>
</TabItem>
</Tabs>
</details>

Expand All @@ -159,5 +174,16 @@ DELETE FROM reviews WHERE bookId = '0786222727';
```
</div>
</TabItem>
<TabItem value="csharp" label="C#">
<div>
```csharp
IMongoCollection<Review> reviewsCollection = db.GetCollection<Review>("reviews");

var deletionResult = reviewsCollection.DeleteMany(r => r.BookId == "0786222727");

Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted.");
```
</div>
</TabItem>
</Tabs>
</details>
17 changes: 15 additions & 2 deletions docs/40-CRUD/5-UPDATE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Executing the above command will insert a fresh new document in the collection,
<div>
```js
await books.updateOne(
{"title": "Treasure of the Sun"},
{"title": "Treasure of the Sun"},
{$set: {pages: 449}}
);
```
Expand All @@ -100,11 +100,24 @@ Executing the above command will insert a fresh new document in the collection,
<div>
```js
db.books.updateOne(
{"title": "Treasure of the Sun"},
{"title": "Treasure of the Sun"},
{$set: {pages: 449}}
);
```
</div>
</TabItem>
<TabItem value="csharp" label="C#">
<div>
```csharp
var filter = Builders<Book>.Filter.Eq(b => b.Title, "Treasure of the Sun");
var update = Builders<Book>.Update.Set(b => b.Pages, 449);

var result = booksCollection.UpdateOne(filter, update);

// Optionally inspect the outcome
Console.WriteLine($"Matched: {result.MatchedCount}, Modified: {result.ModifiedCount}");
```
</div>
</TabItem>
</Tabs>
</details>
45 changes: 45 additions & 0 deletions docs/50-aggregation/2-match-project.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ db.books.aggregate([
```
</div>
</TabItem>
<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");
}
```
</div>
</TabItem>
</Tabs>
</details>

Expand Down Expand Up @@ -146,5 +168,28 @@ db.books.aggregate([
```
</div>
</TabItem>
<TabItem value="csharp" label="C#">
<div>
```csharp
var pipeline = booksCollection.Aggregate()
.Match(b => b.Available > 2)
.Project(b => new { b.Title, b.Year });

var plentifulBooks = pipeline.ToList();

if(plentifulBooks != null)
{
foreach(var book in plentifulBooks)
{
Console.WriteLine($"Title: {book.Title} - Year: {book.Year}");
}
}
else
{
Console.WriteLine("Empty Collection");
}
```
</div>
</TabItem>
</Tabs>
</details>
153 changes: 100 additions & 53 deletions docs/50-aggregation/3-sort-limit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,67 +77,114 @@ SELECT * FROM books ORDER BY timestamp DESC LIMIT 5;
<details>
<summary>Answer</summary>
<div>
There are 2 ways to solve this-
There are 2 ways to solve this-
- $project
- $addFields

:::info
Learn [when to use $addFields over $project](https://www.practical-mongodb-aggregations.com/guides/project.html?highlight=%24project#when-to-use-project)
:::

<Tabs groupId="aggregations">
<TabItem value="mongodb-shell" label="Using $project">
```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
}
]).toArray();
```
<Tabs groupId="aggregations" defaultValue="js">
<TabItem value="js" label="JavaScript">
<Tabs groupId="js-inner" defaultValue="project">
<TabItem value="project" label="Using $project">
```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
}
]).toArray();
```
</TabItem>

<TabItem value="addFields" label="Using $addFields">
```js
await books.aggregate([
{
$match: { year: { $gt: 2000 } }
},
{
$match: {
authors: { $exists: true },
}
},
{
$addFields: {
numAuthors: { $size: "$authors" },
}
},
{
$sort: { "numAuthors": -1 }
},
{
$limit: 1
}
]).toArray();
```
</TabItem>
</Tabs>
</TabItem>
<TabItem value="atlas" label="Using $addFields">
```js
await books.aggregate([
{
$match: { year: { $gt: 2000 } }
},
{
$match: {
authors: { $exists: true },
}
},
{
$addFields: {
numAuthors: { $size: "$authors" },
}
},
{
$sort: { "numAuthors": -1 }
},
{
$limit: 1
}
]).toArray();
```

<TabItem value="csharp" label="C#">
<Tabs groupId="aggregations-csharp" defaultValue="addFields">
<TabItem value="project" label="Using $project">
<div>
```csharp
var pipeline = booksCollection.Aggregate()
.Match(b => b.Year > 2000)
.Match(Builders<Book>.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();
```
</div>
</TabItem>

<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")))
)
.Sort(new BsonDocument("numAuthors", -1))
.Limit(1);

var mostAuthors = pipeline.ToList();
```
</div>
</TabItem>
</Tabs>
</TabItem>
</Tabs>
</div>
Expand Down
Loading