Skip to content

Commit 7a85c99

Browse files
authored
[C#] CodeWhisperer Code Comments - Part 2 (#151)
* Codewhisperer comments p2
1 parent ca0fa91 commit 7a85c99

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

source/includes/code-examples/insert-many/InsertMany.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Inserts sample documents describing restaurants by using the C# driver
2+
13
using MongoDB.Bson;
24
using MongoDB.Bson.Serialization.Conventions;
35
using MongoDB.Driver;
@@ -13,56 +15,60 @@ public static void Main(string[] args)
1315
{
1416
Setup();
1517

16-
// Attempt to find document before insert
18+
// Creates a filter for all documents that have a "name" value of "Mongo's Pizza"
1719
var filter = Builders<Restaurant>.Filter
1820
.Eq(r => r.Name, "Mongo's Pizza");
1921

22+
// Finds all documents that match the filter
2023
var foundRestaurants = _restaurantsCollection.Find(filter).ToList();
2124

2225
Console.WriteLine($"Number of restaurants found before insert: {foundRestaurants.Count}");
2326

24-
// Extra space for console readability
27+
// Prints extra space for console readability
2528
Console.WriteLine();
2629

2730
Console.WriteLine("Inserting documents...");
31+
32+
// Inserts the documents by using a helper method
2833
InsertManyRestaurants();
2934

30-
// Find and print newly inserted document
35+
// Finds all documents that match the filter after the insert
3136
foundRestaurants = _restaurantsCollection.Find(filter).ToList();
3237

38+
// Prints the number of documents found
3339
Console.WriteLine($"Number of restaurants inserted: {foundRestaurants.Count}");
3440

3541
Cleanup();
3642
}
3743

3844
private static void InsertManyRestaurants()
3945
{
40-
// Delete sample document if already exists
4146
Cleanup();
4247

4348
// start-insert-many
44-
// Helper method to generate 5 new restaurants
49+
// Generates 5 new restaurants by using a helper method
4550
var restaurants = GenerateDocuments();
4651

52+
// Inserts the new documents into the restaurants collection
4753
_restaurantsCollection.InsertMany(restaurants);
4854
// end-insert-many
4955
}
5056

5157
private static void Setup()
5258
{
53-
// This allows automapping of the camelCase database fields to our models.
59+
// Allows automapping of the camelCase database fields to models
5460
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
5561
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
5662

57-
// Establish the connection to MongoDB and get the restaurants database
63+
// Establishes the connection to MongoDB and accesses the restaurants database
5864
var mongoClient = new MongoClient(MongoConnectionString);
5965
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
6066
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
6167
}
6268

6369
private static List<Restaurant> GenerateDocuments()
6470
{
65-
// Generate 5 new restaurant documents
71+
// Generates 5 new restaurant documents
6672
var restaurantsList = new List<Restaurant>();
6773
for (int i = 1; i <= 5; i++)
6874
{

source/includes/code-examples/insert-many/InsertManyAsync.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Asynchronously inserts sample documents describing restaurants by using the C# driver
2+
13
using MongoDB.Bson;
24
using MongoDB.Bson.Serialization.Conventions;
35
using MongoDB.Driver;
@@ -13,56 +15,61 @@ public static async Task Main(string[] args)
1315
{
1416
Setup();
1517

16-
// Attempt to find document before insert
18+
// Creates a filter for all documents that have a "name" value of "Mongo's Pizza"
1719
var filter = Builders<Restaurant>.Filter
1820
.Eq(r => r.Name, "Mongo's Pizza");
1921

22+
// Finds all documents that match the filter
2023
var foundRestaurants = _restaurantsCollection.Find(filter).ToList();
2124

25+
// Prints the number of documents found
2226
Console.WriteLine($"Number of restaurants found before insert: {foundRestaurants.Count}");
2327

24-
// Extra space for console readability
28+
// Prints extra space for console readability
2529
Console.WriteLine();
2630

2731
Console.WriteLine("Inserting documents...");
32+
33+
// Asynchronously inserts the documents by using a helper method
2834
await InsertManyRestaurantsAsync();
2935

30-
// Find and print newly inserted document
36+
// Finds all documents that match the filter after the insert
3137
foundRestaurants = _restaurantsCollection.Find(filter).ToList();
3238

39+
// Prints the number of documents found
3340
Console.WriteLine($"Number of restaurants inserted: {foundRestaurants.Count}");
3441

3542
Cleanup();
3643
}
3744

3845
private static async Task InsertManyRestaurantsAsync()
3946
{
40-
// Delete sample document if already exists
4147
Cleanup();
4248

4349
// start-insert-many
44-
// Helper method to generate 5 new restaurants
50+
// Generates 5 new restaurants by using a helper method
4551
var restaurants = GenerateDocuments();
4652

53+
// Asynchronously inserts the new documents into the restaurants collection
4754
await _restaurantsCollection.InsertManyAsync(restaurants);
4855
// end-insert-many
4956
}
5057

5158
private static void Setup()
5259
{
53-
// This allows automapping of the camelCase database fields to our models.
60+
// Allows automapping of the camelCase database fields to models
5461
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
5562
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
5663

57-
// Establish the connection to MongoDB and get the restaurants database
64+
// Establishes the connection to MongoDB and accesses the restaurants database
5865
var mongoClient = new MongoClient(MongoConnectionString);
5966
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
6067
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
6168
}
6269

6370
private static List<Restaurant> GenerateDocuments()
6471
{
65-
// Generate 5 new restaurant documents
72+
// Generates 5 new restaurant documents
6673
var restaurantsList = new List<Restaurant>();
6774
for (int i = 1; i <= 5; i++)
6875
{

source/includes/code-examples/insert-one/InsertOne.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Inserts a sample document describing a restaurant by using the C# driver
2+
13
using MongoDB.Bson;
24
using MongoDB.Bson.Serialization.Conventions;
35
using MongoDB.Driver;
@@ -16,23 +18,25 @@ public static void Main(string[] args)
1618
Console.WriteLine("Inserting a document...");
1719
InsertOneRestaurant();
1820

21+
// Creates a filter for all documents that have a "name" value of "Mongo's Pizza"
1922
var filter = Builders<Restaurant>.Filter
2023
.Eq(r => r.Name, "Mongo's Pizza");
2124

22-
// find and print newly inserted document
25+
// Finds the newly inserted document by using the filter
2326
var document = _restaurantsCollection.Find(filter).FirstOrDefault();
2427

28+
// Prints the document
2529
Console.WriteLine($"Document Inserted: {document.ToBsonDocument()}");
2630

2731
Cleanup();
2832
}
2933

3034
private static void InsertOneRestaurant()
3135
{
32-
// delete sample document if already exists
3336
Cleanup();
3437

3538
// start-insert-one
39+
// Generates a new restaurant document
3640
Restaurant newRestaurant = new()
3741
{
3842
Name = "Mongo's Pizza",
@@ -46,17 +50,18 @@ private static void InsertOneRestaurant()
4650
Borough = "Manhattan",
4751
};
4852

53+
// Inserts the new document into the restaurants collection
4954
_restaurantsCollection.InsertOne(newRestaurant);
5055
// end-insert-one
5156
}
5257

5358
private static void Setup()
5459
{
55-
// This allows automapping of the camelCase database fields to our models.
60+
// Allows automapping of the camelCase database fields to models
5661
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
5762
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
5863

59-
// Establish the connection to MongoDB and get the restaurants database
64+
// Establishes the connection to MongoDB and accesses the restaurants database
6065
var mongoClient = new MongoClient(MongoConnectionString);
6166
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
6267
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

source/includes/code-examples/insert-one/InsertOneAsync.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Asynchronously inserts a sample document describing a restaurant by using the C# driver
2+
13
using MongoDB.Bson;
24
using MongoDB.Bson.Serialization.Conventions;
35
using MongoDB.Driver;
@@ -16,23 +18,25 @@ public static async Task Main(string[] args)
1618
Console.WriteLine("Inserting a document...");
1719
await InsertOneRestaurantAsync();
1820

19-
// find and print newly inserted document
21+
// Creates a filter for all documents that have a "name" value of "Mongo's Pizza"
2022
var filter = Builders<Restaurant>.Filter
2123
.Eq(r => r.Name, "Mongo's Pizza");
2224

25+
// Finds the newly inserted document by using the filter
2326
var document = _restaurantsCollection.Find(filter).FirstOrDefault();
2427

28+
// Prints the document
2529
Console.WriteLine($"Document Inserted: {document.ToBsonDocument()}");
2630

2731
Cleanup();
2832
}
2933

3034
private static async Task InsertOneRestaurantAsync()
3135
{
32-
// delete sample document if already exists
3336
Cleanup();
3437

3538
// start-insert-one-async
39+
// Generates a new restaurant document
3640
Restaurant newRestaurant = new()
3741
{
3842
Name = "Mongo's Pizza",
@@ -46,17 +50,18 @@ private static async Task InsertOneRestaurantAsync()
4650
Borough = "Manhattan",
4751
};
4852

53+
// Asynchronously inserts the new document into the restaurants collection
4954
await _restaurantsCollection.InsertOneAsync(newRestaurant);
5055
// end-insert-one-async
5156
}
5257

5358
private static void Setup()
5459
{
55-
// This allows automapping of the camelCase database fields to our models.
60+
// Allows automapping of the camelCase database fields to models
5661
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
5762
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
5863

59-
// Establish the connection to MongoDB and get the restaurants database
64+
// Establishes the connection to MongoDB and accesses the restaurants database
6065
var mongoClient = new MongoClient(MongoConnectionString);
6166
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
6267
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

0 commit comments

Comments
 (0)