Skip to content

Commit 33aa7d8

Browse files
authored
[C#] CodeWhisperer Code Comments - Part 4 (#154)
* Codewhisperer comments p4
1 parent 53e08ba commit 33aa7d8

File tree

6 files changed

+54
-103
lines changed

6 files changed

+54
-103
lines changed

source/fundamentals/crud/write-operations/modify.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ The {+driver-short+} provides a ``Builders`` class that simplifies the creation
9696
query filters and update documents. The following code sample uses ``Builders`` to create
9797
two documents for use as parameters in an update operation:
9898

99-
- A query filter that searches for restaurants with a ``borough`` field value of "Manhattan"
100-
- An update document that sets the value of the ``borough`` field of these restaurants
101-
to "Manhattan (north)"
99+
- A query filter that searches for restaurants with a ``cuisine`` field value of "Pizza"
100+
- An update document that sets the value of the ``cuisine`` field of these restaurants
101+
to "Pasta and breadsticks"
102102

103-
.. literalinclude:: /includes/fundamentals/code-examples/crud/change/Update.cs
103+
.. literalinclude:: /includes/code-examples/update-many/UpdateMany.cs
104104
:language: csharp
105105
:dedent:
106-
:start-after: start-builders
107-
:end-before: end-builders
106+
:start-after: // start-update-many
107+
:end-before: // end-update-many
108108

109109
.. tip:: Aggregation Pipelines in Update Operations
110110

source/includes/code-examples/update-many/UpdateMany.cs

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

@@ -16,20 +18,20 @@ public static void Main(string[] args)
1618
{
1719
Setup();
1820

19-
// Extra space for console readability
21+
// Prints extra space for console readability
2022
Console.WriteLine();
2123

22-
// Number of restaurants with old cuisine
24+
// Finds the number of restaurants with a "cuisine" value of "Pizza"
2325
Console.WriteLine($"Restaurants with {CuisineField} \"{OldCuisine}\" found: {FindCountOfRestaurantsWithCuisine(OldCuisine)}");
2426

25-
// Update many documents synchronously
27+
// Updates many documents by using a helper method
2628
var syncResult = UpdateManyRestaurants();
2729
Console.WriteLine($"Restaurants modified by update: {syncResult.ModifiedCount}");
2830

29-
// Number of restaurants with new cuisine
31+
// Finds the number of restaurants with a "cuisine" value of "Pasta and breadsticks"
3032
Console.WriteLine($"Restaurants with {CuisineField} \"{NewCuisine}\" found after update: {FindCountOfRestaurantsWithCuisine(NewCuisine)}");
3133

32-
// Reset sample data
34+
// Resets the sample data
3335
Console.WriteLine("Resetting sample data...");
3436
ResetSampleData();
3537
Console.WriteLine("done.");
@@ -41,12 +43,16 @@ private static UpdateResult UpdateManyRestaurants()
4143
const string oldValue = "Pizza";
4244
const string newValue = "Pasta and breadsticks";
4345

44-
var filter = Builders<Restaurant>.Filter
46+
// Creates a filter for all documents with a "cuisine" value of "Pizza"
47+
var filter = Builders<Restaurant>.Filter
4548
.Eq(restaurant => restaurant.Cuisine, oldValue);
4649

50+
// Creates instructions to update the "cuisine" field of documents that
51+
// match the filter
4752
var update = Builders<Restaurant>.Update
4853
.Set(restaurant => restaurant.Cuisine, newValue);
4954

55+
// Updates all documents that have a "cuisine" value of "Pizza"
5056
return _restaurantsCollection.UpdateMany(filter, update);
5157
// end-update-many
5258
}
@@ -61,11 +67,11 @@ private static long FindCountOfRestaurantsWithCuisine(string cuisineValue)
6167

6268
private static void Setup()
6369
{
64-
// This allows automapping of the camelCase database fields to our models.
70+
// Allows automapping of the camelCase database fields to models
6571
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
6672
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
6773

68-
// Establish the connection to MongoDB and get the restaurants database
74+
// Establishes the connection to MongoDB and accesses the "sample_restaurants" collection
6975
var mongoClient = new MongoClient(MongoConnectionString);
7076
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
7177
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

source/includes/code-examples/update-many/UpdateManyAsync.cs

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

@@ -16,20 +18,20 @@ public static async Task Main(string[] args)
1618
{
1719
Setup();
1820

19-
// Extra space for console readability
21+
// Prints extra space for console readability
2022
Console.WriteLine();
2123

22-
// Number of restaurants with old cuisine
24+
// Finds the number of restaurants with a "cuisine" value of "Pizza"
2325
Console.WriteLine($"Restaurants with {CuisineField} \"{OldCuisine}\" found: {FindCountOfRestaurantsWithCuisine(OldCuisine)}");
2426

25-
// Update many documents synchronously
27+
// Asynchronously updates many documents by using a helper method
2628
var asyncResult = await UpdateManyRestaurantsAsync();
2729
Console.WriteLine($"Restaurants modified by update: {asyncResult.ModifiedCount}");
2830

29-
// Number of restaurants with new cuisine
31+
// Finds the number of restaurants with a "cuisine" value of "Pasta and breadsticks"
3032
Console.WriteLine($"Restaurants with {CuisineField} \"{NewCuisine}\" found after update: {FindCountOfRestaurantsWithCuisine(NewCuisine)}");
3133

32-
// Reset sample data
34+
// Resets the sample data
3335
Console.WriteLine("Resetting sample data...");
3436
ResetSampleData();
3537
Console.WriteLine("done.");
@@ -41,12 +43,16 @@ private static async Task<UpdateResult> UpdateManyRestaurantsAsync()
4143
const string oldValue = "Pizza";
4244
const string newValue = "Pasta and breadsticks";
4345

44-
var filter = Builders<Restaurant>.Filter
46+
// Creates a filter for all documents with a "cuisine" value of "Pizza"
47+
var filter = Builders<Restaurant>.Filter
4548
.Eq(restaurant => restaurant.Cuisine, oldValue);
4649

50+
// Creates instructions to update the "cuisine" field of documents that
51+
// match the filter
4752
var update = Builders<Restaurant>.Update
4853
.Set(restaurant => restaurant.Cuisine, newValue);
4954

55+
// Updates all documents that have a "cuisine" value of "Pizza"
5056
return await _restaurantsCollection.UpdateManyAsync(filter, update);
5157
// end-update-many-async
5258
}
@@ -61,11 +67,11 @@ private static long FindCountOfRestaurantsWithCuisine(string cuisineValue)
6167

6268
private static void Setup()
6369
{
64-
// This allows automapping of the camelCase database fields to our models.
70+
// Allows automapping of the camelCase database fields to models
6571
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
6672
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
6773

68-
// Establish the connection to MongoDB and get the restaurants database
74+
// Establishes the connection to MongoDB and accesses the "sample_restaurants" collection
6975
var mongoClient = new MongoClient(MongoConnectionString);
7076
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
7177
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

source/includes/code-examples/update-one/UpdateOne.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Updates the first document that matches a query filter by using the C# driver
2+
13
using MongoDB.Bson.Serialization.Conventions;
24
using MongoDB.Driver;
35

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

15-
// Extra space for console readability
17+
// Prints extra space for console readability
1618
Console.WriteLine();
1719

18-
// Update one document synchronously
20+
// Updates one document by using a helper method
1921
var syncResult = UpdateOneRestaurant();
2022
Console.WriteLine($"Updated documents: {syncResult.ModifiedCount}");
2123
ResetSampleData();
@@ -27,23 +29,27 @@ private static UpdateResult UpdateOneRestaurant()
2729
const string oldValue = "Bagels N Buns";
2830
const string newValue = "2 Bagels 2 Buns";
2931

32+
// Creates a filter for all documents with a "name" of "Bagels N Buns"
3033
var filter = Builders<Restaurant>.Filter
3134
.Eq(restaurant => restaurant.Name, oldValue);
3235

36+
// Creates instructions to update the "name" field of the first document
37+
// that matches the filter
3338
var update = Builders<Restaurant>.Update
3439
.Set(restaurant => restaurant.Name, newValue);
3540

41+
// Updates the first document that has a "name" value of "Bagels N Buns"
3642
return _restaurantsCollection.UpdateOne(filter, update);
3743
// end-update-one
3844
}
3945

4046
private static void Setup()
4147
{
42-
// This allows automapping of the camelCase database fields to our models.
48+
// Allows automapping of the camelCase database fields to models
4349
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
4450
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
4551

46-
// Establish the connection to MongoDB and get the restaurants database
52+
// Establishes the connection to MongoDB and accesses the "sample_restaurants" collection
4753
var mongoClient = new MongoClient(MongoConnectionString);
4854
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
4955
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

source/includes/code-examples/update-one/UpdateOneAsync.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Asynchronously updates the first document that matches a query filter by using the C# driver
2+
13
using MongoDB.Bson.Serialization.Conventions;
24
using MongoDB.Driver;
35

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

15-
// Extra space for console readability
17+
// Prints extra space for console readability
1618
Console.WriteLine();
1719

18-
//Update one document asynchronously
20+
// Updates one document asynchronously by using a helper method
1921
var asyncResult = await UpdateOneRestaurantAsync();
2022
Console.WriteLine($"Updated documents: {asyncResult.ModifiedCount}");
2123
ResetSampleData();
@@ -27,23 +29,27 @@ private static async Task<UpdateResult> UpdateOneRestaurantAsync()
2729
const string oldValue = "Bagels N Buns";
2830
const string newValue = "2 Bagels 2 Buns";
2931

32+
// Creates a filter for all documents with a "name" of "Bagels N Buns"
3033
var filter = Builders<Restaurant>.Filter
3134
.Eq(restaurant => restaurant.Name, oldValue);
3235

36+
// Creates instructions to update the "name" field of the first document
37+
// that matches the filter
3338
var update = Builders<Restaurant>.Update
3439
.Set(restaurant => restaurant.Name, newValue);
3540

41+
// Updates the first document that has a "name" value of "Bagels N Buns"
3642
return await _restaurantsCollection.UpdateOneAsync(filter, update);
3743
// end-update-one-async
3844
}
3945

4046
private static void Setup()
4147
{
42-
// This allows automapping of the camelCase database fields to our models.
48+
// Allows automapping of the camelCase database fields to models
4349
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
4450
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
4551

46-
// Establish the connection to MongoDB and get the restaurants database
52+
// Establishes the connection to MongoDB and accesses the "sample_restaurants" collection
4753
var mongoClient = new MongoClient(MongoConnectionString);
4854
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
4955
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");

source/includes/fundamentals/code-examples/crud/change/Update.cs

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)