Skip to content

Commit ca0fa91

Browse files
authored
[C#] CodeWhisperer Code Comments - Part 1 (#149)
* CodeWhisperer Code Comments for DeleteMany, DeleteOne, FindMany and FindOne, both sync and async
1 parent 9ec1bae commit ca0fa91

File tree

8 files changed

+73
-35
lines changed

8 files changed

+73
-35
lines changed

source/includes/code-examples/delete-many/DeleteMany.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Deletes multiple documents from a collection by using the C# driver
2+
13
using MongoDB.Bson.Serialization.Conventions;
24
using MongoDB.Driver;
35

@@ -17,7 +19,7 @@ public static void Main(string[] args)
1719

1820
var docs = _restaurantsCollection.Find(filter).ToList();
1921

20-
// Deleting documents using builders
22+
// Deletes documents by using builders
2123
Console.WriteLine("Deleting documents...");
2224
var result = DeleteMultipleRestaurantsBuilder();
2325

@@ -26,12 +28,16 @@ public static void Main(string[] args)
2628
Restore(docs);
2729
}
2830

31+
// Deletes all documents that have a Borough value of "Brooklyn"
2932
private static DeleteResult DeleteMultipleRestaurantsBuilder()
3033
{
31-
// start-delete-many-builders
34+
// start-delete-many-builders
35+
// Creates a filter for all documents that have a
36+
// "borough" value of "Brooklyn"
3237
var filter = Builders<Restaurant>.Filter
3338
.Eq(r => r.Borough, "Brooklyn");
3439

40+
// Deletes all documents that match the filter
3541
return _restaurantsCollection.DeleteMany(filter);
3642
// end-delete-many-builders
3743
}
@@ -44,11 +50,11 @@ private static void Restore(IEnumerable<Restaurant> docs)
4450

4551
private static void Setup()
4652
{
47-
// This allows automapping of the camelCase database fields to our models.
53+
// Allows automapping of the camelCase database fields to models
4854
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
4955
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
5056

51-
// Establish the connection to MongoDB and get the restaurants database
57+
// Establishes the connection to MongoDB and accesses the "restaurants" collection
5258
var mongoClient = new MongoClient(MongoConnectionString);
5359
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
5460
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

source/includes/code-examples/delete-many/DeleteManyAsync.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Asynchronously deletes multiple documents from a collection by using the C# driver
2+
13
using MongoDB.Bson.Serialization.Conventions;
24
using MongoDB.Driver;
35

@@ -17,7 +19,7 @@ public static async Task Main(string[] args)
1719

1820
var docs = _restaurantsCollection.Find(filter).ToList();
1921

20-
// Deleting documents using builders
22+
// Deletes documents by using builders
2123
Console.WriteLine("Deleting documents...");
2224
var result = await DeleteMultipleRestaurantsBuilderAsync();
2325

@@ -28,12 +30,16 @@ public static async Task Main(string[] args)
2830
return result;
2931
}
3032

33+
// Deletes all documents that have a Borough value of "Brooklyn"
3134
private static async Task<DeleteResult> DeleteMultipleRestaurantsBuilderAsync()
3235
{
3336
// start-delete-many-async
37+
// Creates a filter for all documents that have a
38+
// "borough" value of "Brooklyn"
3439
var filter = Builders<Restaurant>.Filter
3540
.Eq(r => r.Borough, "Brooklyn");
3641

42+
// Asynchronously deletes all documents that match the filter
3743
return await _restaurantsCollection.DeleteManyAsync(filter);
3844
// end-delete-many-async
3945
}
@@ -46,11 +52,11 @@ private static void Restore(IEnumerable<Restaurant> docs)
4652

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

53-
// Establish the connection to MongoDB and get the restaurants database
59+
// Establishes the connection to MongoDB and accesses the "restaurants" collection
5460
var mongoClient = new MongoClient(MongoConnectionString);
5561
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
5662
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

source/includes/code-examples/delete-one/DeleteOne.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Deletes a document from a collection by using the C# driver
2+
13
using MongoDB.Bson.Serialization.Conventions;
24
using MongoDB.Driver;
35

@@ -17,7 +19,7 @@ public static void Main(string[] args)
1719

1820
var doc = _restaurantsCollection.Find(filter).First();
1921

20-
// Delete a document using builders
22+
// Deletes a document by using builders
2123
Console.WriteLine("Deleting a document with builders...");
2224
var result = DeleteARestaurantBuilder();
2325

@@ -29,9 +31,11 @@ public static void Main(string[] args)
2931
private static DeleteResult DeleteARestaurantBuilder()
3032
{
3133
// start-delete-one-builders
34+
// Creates a filter for all documents that have a "name" value of "Ready Penny Inn"
3235
var filter = Builders<Restaurant>.Filter
3336
.Eq(r => r.Name, "Ready Penny Inn");
3437

38+
// Deletes the first document that matches the filter
3539
return _restaurantsCollection.DeleteOne(filter);
3640
// end-delete-one-builders
3741
}
@@ -43,11 +47,11 @@ private static void Restore(Restaurant doc)
4347

4448
private static void Setup()
4549
{
46-
// This allows automapping of the camelCase database fields to our models.
50+
// Allows automapping of the camelCase database fields to models
4751
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
4852
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
4953

50-
// Establish the connection to MongoDB and get the restaurants database
54+
// Establishes the connection to MongoDB and accesses the "restaurants" collection
5155
var mongoClient = new MongoClient(MongoConnectionString);
5256
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
5357
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

source/includes/code-examples/delete-one/DeleteOneAsync.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Asynchronously deletes a document from a collection by using the C# driver
2+
13
using MongoDB.Bson.Serialization.Conventions;
24
using MongoDB.Driver;
35

@@ -17,7 +19,7 @@ public static async Task Main(string[] args)
1719

1820
var doc = _restaurantsCollection.Find(filter).First();
1921

20-
// Delete a document using builders
22+
// Deletes a document by using builders
2123
Console.WriteLine("Deleting a document with builders...");
2224
var result = await DeleteARestaurantBuilderAsync();
2325

@@ -29,9 +31,11 @@ public static async Task Main(string[] args)
2931
private static async Task<DeleteResult> DeleteARestaurantBuilderAsync()
3032
{
3133
// start-delete-one-builders-async
34+
// Creates a filter for all documents that have a "name" value of "Ready Penny Inn"
3235
var filter = Builders<Restaurant>.Filter
3336
.Eq(r => r.Name, "Ready Penny Inn");
3437

38+
// Asynchronously deletes the first document that matches the filter
3539
return await _restaurantsCollection.DeleteOneAsync(filter);
3640
// end-delete-one-builders-async
3741
}
@@ -43,11 +47,11 @@ private static void Restore(Restaurant doc)
4347

4448
private static void Setup()
4549
{
46-
// This allows automapping of the camelCase database fields to our models.
50+
// Allows automapping of the camelCase database fields to models
4751
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
4852
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
4953

50-
// Establish the connection to MongoDB and get the restaurants database
54+
// Establishes the connection to MongoDB and accesses the "restaurants" collection
5155
var mongoClient = new MongoClient(MongoConnectionString);
5256
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
5357
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

source/includes/code-examples/find-many/FindMany.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Retrieves documents that match a query filter by using the C# driver
2+
13
using MongoDB.Bson.Serialization.Conventions;
24
using MongoDB.Driver;
35

@@ -12,22 +14,22 @@ public static void Main(string[] args)
1214
{
1315
Setup();
1416

15-
// Find multiple documents using builders
17+
// Finds multiple documents by using builders
1618
Console.WriteLine("Finding documents with builders...:");
1719
var restaurants = FindMultipleRestaurantsBuilderSync();
1820
Console.WriteLine($"Number of documents found: {restaurants.Count}");
1921

20-
// Extra space for console readability
22+
// Prints extra space for console readability
2123
Console.WriteLine();
2224

23-
// Find multiple documents using LINQ
25+
// Retrieves multiple documents by using LINQ
2426
Console.WriteLine("Finding documents with LINQ...:");
2527
restaurants = FindMultipleRestaurantsLinqSync();
2628
Console.WriteLine($"Number of documents found: {restaurants.Count}");
2729

2830
Console.WriteLine();
2931

30-
// Find all restaurants
32+
// Retrieves all documents in the "restaurants" collection
3133
Console.WriteLine("Finding all documents...:");
3234
restaurants = FindAllRestaurantsSync();
3335
Console.WriteLine($"Number of documents found: {restaurants.Count}");
@@ -36,15 +38,18 @@ public static void Main(string[] args)
3638
public static List<Restaurant> FindMultipleRestaurantsBuilderSync()
3739
{
3840
// start-find-builders-sync
41+
// Creates a filter for all documents that have a "cuisine" value of "Pizza"
3942
var filter = Builders<Restaurant>.Filter
4043
.Eq("cuisine", "Pizza");
4144

45+
// Retrieves all documents that match the filter
4246
return _restaurantsCollection.Find(filter).ToList();
4347
// end-find-builders-sync
4448
}
4549

4650
public static List<Restaurant> FindMultipleRestaurantsLinqSync()
4751
{
52+
// Retrieves all documents with a "cuisine" value of "Pizza" by using a LINQ query
4853
// start-find-linq-sync
4954
return _restaurantsCollection.AsQueryable()
5055
.Where(r => r.Cuisine == "Pizza").ToList();
@@ -63,11 +68,11 @@ private static List<Restaurant> FindAllRestaurantsSync()
6368

6469
private static void Setup()
6570
{
66-
// This allows automapping of the camelCase database fields to our models.
71+
// Allows automapping of the camelCase database fields to models
6772
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
6873
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
6974

70-
// Establish the connection to MongoDB and get the restaurants database
75+
// Establishes the connection to MongoDB and accesses the "restaurants" collection
7176
var mongoClient = new MongoClient(MongoConnectionString);
7277
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
7378
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

source/includes/code-examples/find-many/FindManyAsync.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Asynchronously retrieves documents that match a query filter by using the C# driver
2+
13
using MongoDB.Bson.Serialization.Conventions;
24
using MongoDB.Driver;
35
using MongoDB.Driver.Linq;
@@ -13,22 +15,22 @@ public static async Task Main(string[] args)
1315
{
1416
Setup();
1517

16-
// Find multiple documents using builders
18+
// Finds multiple documents by using builders
1719
Console.WriteLine("Finding documents with builders...:");
1820
var restaurantsBuilders = await FindMultipleRestaurantsBuilderAsync();
1921
Console.WriteLine($"Number of documents found: {restaurantsBuilders.Count}");
2022

21-
// Extra space for console readability
23+
// Prints extra space for console readability
2224
Console.WriteLine();
2325

24-
// Find multiple documents using LINQ
26+
// Retrieves multiple documents by using LINQ
2527
Console.WriteLine("Finding documents with LINQ...:");
2628
var restaurantsLinq = await FindMultipleRestaurantsLinqAsync();
2729
Console.WriteLine($"Number of documents found: {restaurantsLinq.Count}");
2830

2931
Console.WriteLine();
3032

31-
// Find all documents
33+
// Retrieves all documents in the "restaurants" collection
3234
Console.WriteLine("Finding all documents...:");
3335
var allRestaurants = await FindAllRestaurantsAsync();
3436
Console.WriteLine($"Number of documents found: {allRestaurants.Count}");
@@ -37,15 +39,18 @@ public static async Task Main(string[] args)
3739
private static async Task<List<Restaurant>> FindMultipleRestaurantsBuilderAsync()
3840
{
3941
// start-find-builders-async
42+
// Creates a filter for all documents that have a "cuisine" value of "Pizza"
4043
var filter = Builders<Restaurant>.Filter
4144
.Eq(r => r.Cuisine, "Pizza");
4245

46+
// Asynchronously retrieves all documents that match the filter
4347
return await _restaurantsCollection.Find(filter).ToListAsync();
4448
// end-find-builders-async
4549
}
4650

4751
private static async Task<List<Restaurant>> FindMultipleRestaurantsLinqAsync()
4852
{
53+
// Asynchronously retrieves all documents with a "cuisine" value of "Pizza" by using a LINQ query
4954
// start-find-linq-async
5055
return await _restaurantsCollection.AsQueryable()
5156
.Where(r => r.Cuisine == "Pizza").ToListAsync();
@@ -64,11 +69,11 @@ private static async Task<List<Restaurant>> FindAllRestaurantsAsync()
6469

6570
private static void Setup()
6671
{
67-
// This allows automapping of the camelCase database fields to our models.
72+
// Allows automapping of the camelCase database fields to models
6873
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
6974
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
7075

71-
// Establish the connection to MongoDB and get the restaurants database
76+
// Establishes the connection to MongoDB and accesses the "restaurants" collection
7277
var mongoClient = new MongoClient(MongoConnectionString);
7378
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
7479
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

source/includes/code-examples/find-one/FindOne.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Retrieves a document that match a query filter by using the C# driver
2+
13
using MongoDB.Bson;
24
using MongoDB.Bson.Serialization.Conventions;
35
using MongoDB.Driver;
@@ -13,24 +15,26 @@ public static void Main(string[] args)
1315
{
1416
Setup();
1517

16-
// Find one document using builders
18+
// Finds one document by using builders
1719
Console.WriteLine("Finding a document with builders...");
1820
FindOneRestaurantBuilder();
1921

20-
// Extra space for console readability
22+
// Prints extra space for console readability
2123
Console.WriteLine();
2224

23-
// Find one document using LINQ
25+
// Finds one document by using LINQ
2426
Console.WriteLine("Finding a document with LINQ...");
2527
FindOneRestaurantLinq();
2628
}
2729

2830
private static void FindOneRestaurantBuilder()
2931
{
3032
// start-find-builders
33+
// Creates a filter for all documents that have a "name" value of "Bagels N Buns"
3134
var filter = Builders<Restaurant>.Filter
3235
.Eq(r => r.Name, "Bagels N Buns");
3336

37+
// Retrieves the first document that matches the filter
3438
var restaurant = _restaurantsCollection.Find(filter).FirstOrDefault();
3539
// end-find-builders
3640

@@ -49,11 +53,11 @@ private static void FindOneRestaurantLinq()
4953

5054
private static void Setup()
5155
{
52-
// This allows automapping of the camelCase database fields to our models.
56+
// Allows automapping of the camelCase database fields to models
5357
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
5458
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
5559

56-
// Establish the connection to MongoDB and get the restaurants database
60+
// Establishes the connection to MongoDB and accesses the "restaurants" collection
5761
var mongoClient = new MongoClient(MongoConnectionString);
5862
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
5963
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

0 commit comments

Comments
 (0)