Skip to content

Commit da94e55

Browse files
authored
[C#] CodeWhisperer Code Comments - Part 6 (#158)
* Codewhisperer comments p6
1 parent 8949f5e commit da94e55

File tree

7 files changed

+44
-2
lines changed

7 files changed

+44
-2
lines changed

source/includes/fundamentals/code-examples/builders.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,41 @@ public static void Main(string[] args)
1212
{
1313
Setup();
1414

15-
// Sample builders definitions
15+
// Creates a filter for all documents with a "season" value of "Spring"
1616
var filter = Builders<Flower>.Filter.AnyEq(flower => flower.Season, "spring");
17+
18+
// Creates a projection to include the "name" and "price" fields, while
19+
// excluding the "id" field
1720
var projection = Builders<Flower>.Projection.Include("Name").Include("Price").Exclude("Id");
21+
22+
// Creates instructions to sort results by ascending price and to sort
23+
// results with the same price in descending category order
1824
var sort = Builders<Flower>.Sort.Ascending("Price").Descending("Category");
1925

2026
Console.WriteLine("Finding documents...");
27+
28+
// Finds all documents that match the filter and applies the specified
29+
// projection and sort to the results
2130
var result = _flowerCollection.Find(filter).Sort(sort).Project(projection).toList();
2231

2332
Console.WriteLine(result.ToJson());
2433

34+
// Creates an update object for that will multiply the price value by
35+
// 0.9
2536
var update = Builders<Flower>.Update.Mul("Price", 0.9);
37+
38+
// Applies the update object instructions to the first document that
39+
// matches the filter
2640
var result2 = _flowerCollection.UpdateOne(filter, update);
2741
}
2842

2943
private static void Setup()
3044
{
31-
// Establish the connection to MongoDB and get the restaurants database
45+
// Establishes the connection to MongoDB and gets the "plants" database
3246
var mongoClient = new MongoClient(_mongoConnectionString);
3347
var myDatabase = mongoClient.GetDatabase("plants");
48+
49+
// Creates a reference to the "flowers" collection
3450
_flowerCollection = myDatabase.GetCollection<Flower>("flowers");
3551
}
3652
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
// Creates a filter for all documents with an "establishedYear" value greater
2+
// than 1985 and a "make" value that does not equal "Kiesel"
13
var builder = Builders<Guitar>.Filter;
24
var filter = builder.And(builder.Gte(g => g.EstablishedYear, 1985), builder.Ne(r => r.Make, "Kiesel"));
5+
6+
// Finds all documents that match the filter
37
var result = _guitarsCollection.Find(filter).ToList();
48

59
foreach (var doc in result)
610
{
11+
// Prints the documents in bson (json) format
712
Console.WriteLine(doc.ToBsonDocument());
813
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
// Creates a filter for all documents with a "make" value of "Fender"
12
var filter = Builders<Guitar>.Filter.Eq(g => g.Make, "Fender");
3+
4+
// Finds all documents that match the filter
25
var result = _guitarsCollection.Find(filter).ToList();
36

47
foreach (var doc in result)
58
{
9+
// Prints the documents in bson (json) format
610
Console.WriteLine(doc.ToBsonDocument());
711
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
// Creates a filter for all documents with a populated "ratings" field
12
var filter = Builders<Guitar>.Filter.Exists(g => g.Rating);
3+
4+
// Finds all documents that match the filter
25
var result = _guitarsCollection.Find(filter).ToList();
36

47
foreach (var doc in result)
58
{
9+
// Prints the documents in bson (json) format
610
Console.WriteLine(doc.ToBsonDocument());
711
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
// Creates a filter for all documents with an "establishedYear" value greater
2+
// than 1985
13
var filter = Builders<Guitar>.Filter.Gt(g => g.EstablishedYear, 1985);
4+
5+
// Finds all documents that match the filter
26
var result = _guitarsCollection.Find(filter).ToList();
37

48
foreach (var doc in result)
59
{
10+
// Prints the documents in bson (json) format
611
Console.WriteLine(doc.ToBsonDocument());
712
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
// Creates a filter for all documents with a populated "ratings" field
12
var filter = Builders<Guitar>.Filter.Regex(g => g.Make, "^G");
3+
4+
// Finds all documents that match the filter
25
var result = _guitarsCollection.Find(filter).ToList();
36

47
foreach (var doc in result)
58
{
9+
// Prints the documents in bson (json) format
610
Console.WriteLine(doc.ToBsonDocument());
711
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
// Creates a filter for all documents with 3 elements in the "models" field
12
var filter = Builders<Guitar>.Filter.Size(g => g.Models, 3);
3+
4+
// Finds all documents that match the filter
25
var result = _guitarsCollection.Find(filter).ToList();
36

47
foreach (var doc in result)
58
{
9+
// Prints the documents in bson (json) format
610
Console.WriteLine(doc.ToBsonDocument());
711
}

0 commit comments

Comments
 (0)