Skip to content

Commit 152ff3c

Browse files
committed
Added C# solution tabs to all challenge answers
1 parent bf2952d commit 152ff3c

File tree

9 files changed

+408
-131
lines changed

9 files changed

+408
-131
lines changed

docs/40-CRUD/2-SELECT.mdx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,23 @@ Here:
137137
<TabItem value="csharp" label="C#">
138138
<div>
139139
```csharp
140-
var projection = Builders<Book>.Projection.Exclude(b => b.Authors).Exclude(b => b.Id);
141-
booksCollection.Find(b => true).Project<Book>(projection);
140+
var historyGenre = Builders<Book>.Filter.AnyEq(b => b.Genres, "History");
141+
var projection = Builders<Book>.Projection.Exclude(b => b.Id).Exclude(b => b.Authors);
142+
143+
List<Book> sortedBooks = booksCollection.Find(historyGenre)
144+
.Project<Book>(projection).ToList();
145+
146+
if(sortedBooks != null)
147+
{
148+
foreach(var book in sortedBooks)
149+
{
150+
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
151+
}
152+
}
153+
else
154+
{
155+
Console.WriteLine("Empty Collection");
156+
}
142157
```
143158
</div>
144159
</TabItem>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
9090
<div>
9191
```csharp
9292
var projection = Builders<Book>.Projection.Include(b => b.Title).Include(b => b.Pages);
93-
var descendingPagesSort = Builders<Book>.Sort.Ascending("title");
93+
var ascendingTitleSort = Builders<Book>.Sort.Ascending("title");
9494

9595
List<Book> sortedBooksLimitedToTen = booksCollection.Find(b => true) // Empty filter to find all books
9696
.Project<Book>(projection)
97-
.Sort(descendingPagesSort)
97+
.Sort(ascendingTitleSort)
9898
.Limit(10).ToList();
9999
```
100100
</div>

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,22 @@ DELETE FROM reviews WHERE bookId = '0786222727';
135135
]);
136136
```
137137
</div>
138-
</TabItem>
138+
</TabItem>
139+
<TabItem value="csharp" label="C#">
140+
<div>
141+
```csharp
142+
var newReviews = new[]
143+
{
144+
new Review { Text = "Thrilling end.", Rating = 4, Name = "Mark", BookId = "0786222727" },
145+
new Review { Text = "Must read!", Rating = 5, Name = "Raj", BookId = "0786222727" },
146+
new Review { Text = "Very expensive", Rating = 3, Name = "Yun", BookId = "0786222727" },
147+
new Review { Text = "Extremely satisfied with the storyline!", Rating = 5, Name = "Lisa", BookId = "0786222727" }
148+
};
149+
150+
reviewsCollection.InsertMany(newReviews);
151+
```
152+
</div>
153+
</TabItem>
139154
</Tabs>
140155
</details>
141156

@@ -159,5 +174,16 @@ DELETE FROM reviews WHERE bookId = '0786222727';
159174
```
160175
</div>
161176
</TabItem>
177+
<TabItem value="csharp" label="C#">
178+
<div>
179+
```csharp
180+
IMongoCollection<Review> reviewsCollection = db.GetCollection<Review>("reviews");
181+
182+
var deletionResult = reviewsCollection.DeleteMany(r => r.BookId == "0786222727");
183+
184+
Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted.");
185+
```
186+
</div>
187+
</TabItem>
162188
</Tabs>
163189
</details>

docs/40-CRUD/5-UPDATE.mdx

Lines changed: 15 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,11 +100,24 @@ 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
```
107107
</div>
108108
</TabItem>
109+
<TabItem value="csharp" label="C#">
110+
<div>
111+
```csharp
112+
var filter = Builders<Book>.Filter.Eq(b => b.Title, "Treasure of the Sun");
113+
var update = Builders<Book>.Update.Set(b => b.Pages, 449);
114+
115+
var result = booksCollection.UpdateOne(filter, update);
116+
117+
// Optionally inspect the outcome
118+
Console.WriteLine($"Matched: {result.MatchedCount}, Modified: {result.ModifiedCount}");
119+
```
120+
</div>
121+
</TabItem>
109122
</Tabs>
110123
</details>

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,29 @@ 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+
128+
var plentifulBooks = pipeline.ToList();
129+
130+
if(results != null)
131+
{
132+
foreach(var book in plentifulBooks)
133+
{
134+
Console.WriteLine($"Title: {book.Title} - Available: {book.Available}");
135+
}
136+
}
137+
else
138+
{
139+
Console.WriteLine("Empty Collection");
140+
}
141+
```
142+
</div>
143+
</TabItem>
121144
</Tabs>
122145
</details>
123146

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

docs/50-aggregation/3-sort-limit.mdx

Lines changed: 99 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -77,64 +77,114 @@ SELECT * FROM books ORDER BY timestamp DESC LIMIT 5;
7777
<details>
7878
<summary>Answer</summary>
7979
<div>
80-
There are 2 ways to solve this-
80+
There are 2 ways to solve this-
8181
- $project
8282
- $addFields
8383

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

88-
<Tabs groupId="aggregations">
89-
<TabItem value="mongodb-shell" label="Using $project">
90-
```js
91-
await books.aggregate([
92-
{
93-
$match: { year: { $gt: 2000 } }
94-
},
95-
{
96-
$match: {
97-
authors: { $exists: true },
88+
<Tabs groupId="aggregations" defaultValue="js">
89+
<TabItem value="js" label="JavaScript">
90+
<Tabs groupId="js-inner" defaultValue="project">
91+
<TabItem value="project" label="Using $project">
92+
```js
93+
await books.aggregate([
94+
{
95+
$match: { year: { $gt: 2000 } }
96+
},
97+
{
98+
$match: {
99+
authors: { $exists: true }
100+
}
101+
},
102+
{
103+
$project: {
104+
title: 1,
105+
year: 1,
106+
authors: 1,
107+
numAuthors: { $size: "$authors" }
108+
}
109+
},
110+
{
111+
$sort: { numAuthors: -1 }
112+
},
113+
{
114+
$limit: 1
98115
}
99-
},
100-
{
101-
$addFields: {
102-
numAuthors: { $size: "$authors" },
103-
}
104-
},
105-
{
106-
$sort: { "numAuthors": -1 }
107-
},
108-
{
109-
$limit: 1
110-
}
111-
]).toArray();
112-
```
116+
]).toArray();
117+
```
118+
</TabItem>
119+
120+
<TabItem value="addFields" label="Using $addFields">
121+
```js
122+
await books.aggregate([
123+
{
124+
$match: { year: { $gt: 2000 } }
125+
},
126+
{
127+
$match: {
128+
authors: { $exists: true },
129+
}
130+
},
131+
{
132+
$addFields: {
133+
numAuthors: { $size: "$authors" },
134+
}
135+
},
136+
{
137+
$sort: { "numAuthors": -1 }
138+
},
139+
{
140+
$limit: 1
141+
}
142+
]).toArray();
143+
```
144+
</TabItem>
145+
</Tabs>
113146
</TabItem>
114-
<TabItem value="atlas" label="Using $addFields">
115-
```js
116-
await books.aggregate([
117-
{
118-
$match: { year: { $gt: 2000 } }
119-
},
120-
{
121-
$match: {
122-
authors: { $exists: true },
123-
}
124-
},
125-
{
126-
$addFields: {
127-
numAuthors: { $size: "$authors" },
128-
}
129-
},
130-
{
131-
$sort: { "numAuthors": -1 }
132-
},
133-
{
134-
$limit: 1
135-
}
136-
]).toArray();
137-
```
147+
148+
<TabItem value="csharp" label="C#">
149+
<Tabs groupId="aggregations-csharp" defaultValue="addFields">
150+
<TabItem value="project" label="Using $project">
151+
<div>
152+
```csharp
153+
var pipeline = booksCollection.Aggregate()
154+
.Match(b => b.Year > 2000)
155+
.Match(Builders<Book>.Filter.Exists(b => b.Authors))
156+
.Project(new BsonDocument
157+
{
158+
{ "title", 1 },
159+
{ "year", 1 },
160+
{ "authors", 1 },
161+
{ "numAuthors", new BsonDocument("$size", "$authors") }
162+
})
163+
.Sort(new BsonDocument("numAuthors", -1))
164+
.Limit(1);
165+
166+
var mostAuthors = await pipeline.ToListAsync();
167+
```
168+
</div>
169+
</TabItem>
170+
171+
<TabItem value="addFields" label="Using $addFields">
172+
<div>
173+
```csharp
174+
var pipeline = booksCollection.Aggregate()
175+
.Match(b => b.Year > 2000)
176+
.Match(Builders<Book>.Filter.Exists(b => b.Authors))
177+
.AppendStage<BsonDocument>(
178+
new BsonDocument("$addFields", new BsonDocument("numAuthors", new BsonDocument("$size", "$authors")))
179+
)
180+
.Sort(new BsonDocument("numAuthors", -1))
181+
.Limit(1);
182+
183+
var mostAuthors = pipeline.ToList();
184+
```
185+
</div>
186+
</TabItem>
187+
</Tabs>
138188
</TabItem>
139189
</Tabs>
140190
</div>

0 commit comments

Comments
 (0)