Skip to content

Commit 7e9f68d

Browse files
authored
Merge pull request #31 from mongodb-developer/add-dotnet-solutions
Added C# tabs and solutions to challenge answers
2 parents 574b298 + 9f429a1 commit 7e9f68d

File tree

10 files changed

+457
-133
lines changed

10 files changed

+457
-133
lines changed

docs/40-CRUD/2-SELECT.mdx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Here:
8888

8989
await cursor.forEach((b) => {
9090
console.log(b);
91-
});
91+
});
9292
```
9393
</div>
9494

@@ -100,6 +100,27 @@ Here:
100100
```
101101
</div>
102102
</TabItem>
103+
<TabItem value="csharp" label="C#">
104+
<div>
105+
```csharp
106+
var projection = Builders<Book>.Projection.Include(b => b.Title).Exclude(b => b.Id);
107+
108+
var booksWithOnlyTitle = booksCollection.Find(b => true).Project<Book>(projection).Limit(50).ToList();
109+
110+
if(booksWithOnlyTitle != null)
111+
{
112+
foreach(var book in booksWithOnlyTitle)
113+
{
114+
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
115+
}
116+
}
117+
else
118+
{
119+
Console.WriteLine("Empty Collection");
120+
}
121+
```
122+
</div>
123+
</TabItem>
103124
</Tabs>
104125
</details>
105126

@@ -126,5 +147,28 @@ Here:
126147
```
127148
</div>
128149
</TabItem>
150+
<TabItem value="csharp" label="C#">
151+
<div>
152+
```csharp
153+
var historyGenre = Builders<Book>.Filter.AnyEq(b => b.Genres, "History");
154+
var projection = Builders<Book>.Projection.Exclude(b => b.Id).Exclude(b => b.Authors);
155+
156+
List<Book> sortedBooks = booksCollection.Find(historyGenre)
157+
.Project<Book>(projection).Limit(50).ToList();
158+
159+
if(sortedBooks != null)
160+
{
161+
foreach(var book in sortedBooks)
162+
{
163+
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
164+
}
165+
}
166+
else
167+
{
168+
Console.WriteLine("Empty Collection");
169+
}
170+
```
171+
</div>
172+
</TabItem>
129173
</Tabs>
130174
</details>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,18 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
8686
```
8787
</div>
8888
</TabItem>
89+
<TabItem value="csharp" label="C#">
90+
<div>
91+
```csharp
92+
var projection = Builders<Book>.Projection.Include(b => b.Title).Include(b => b.Pages);
93+
var ascendingTitleSort = Builders<Book>.Sort.Ascending("title");
94+
95+
List<Book> topBooks = booksCollection.Find(b => true) // Empty filter to find all books
96+
.Project<Book>(projection)
97+
.Sort(ascendingTitleSort)
98+
.Limit(10).ToList();
99+
```
100+
</div>
101+
</TabItem>
89102
</Tabs>
90103
</details>

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

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

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

Lines changed: 100 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -77,67 +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 }
98-
}
99-
},
100-
{
101-
$project: {
102-
title: 1,
103-
year: 1,
104-
authors: 1,
105-
numAuthors: { $size: "$authors" }
106-
}
107-
},
108-
{
109-
$sort: { numAuthors: -1 }
110-
},
111-
{
112-
$limit: 1
113-
}
114-
]).toArray();
115-
```
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
115+
}
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>
116146
</TabItem>
117-
<TabItem value="atlas" label="Using $addFields">
118-
```js
119-
await books.aggregate([
120-
{
121-
$match: { year: { $gt: 2000 } }
122-
},
123-
{
124-
$match: {
125-
authors: { $exists: true },
126-
}
127-
},
128-
{
129-
$addFields: {
130-
numAuthors: { $size: "$authors" },
131-
}
132-
},
133-
{
134-
$sort: { "numAuthors": -1 }
135-
},
136-
{
137-
$limit: 1
138-
}
139-
]).toArray();
140-
```
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>
141188
</TabItem>
142189
</Tabs>
143190
</div>

0 commit comments

Comments
 (0)