Skip to content

Commit 1f23b30

Browse files
authored
[C#] CodeWhisperer Code Comments - Part 3 (#152)
* Codewhisperer comment p3
1 parent 7a85c99 commit 1f23b30

File tree

4 files changed

+32
-94
lines changed

4 files changed

+32
-94
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ Modify Documents
1010
:depth: 2
1111
:class: singlecol
1212

13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: update, replace, synchronous, asynchronous, bulk
19+
1320
Overview
1421
--------
1522

@@ -321,11 +328,11 @@ The following code sample uses ``Builders`` to create a query filter that search
321328
for restaurants with a ``name`` field value of "Pizza Town". The code also creates a new
322329
``Restaurant`` object that will replace the first matched document.
323330

324-
.. literalinclude:: /includes/fundamentals/code-examples/crud/change/Replace.cs
331+
.. literalinclude:: /includes/code-examples/replace-one/ReplaceOne.cs
325332
:language: csharp
326333
:dedent:
327-
:start-after: // start-parameters
328-
:end-before: // end-parameters
334+
:start-after: // start-replace-one
335+
:end-before: // end-replace-one
329336

330337
.. important::
331338

source/includes/code-examples/replace-one/ReplaceOne.cs

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

16-
// Create filter
18+
// Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza"
1719
var filter = Builders<Restaurant>.Filter
1820
.Eq(r => r.Cuisine, "Pizza");
1921

20-
// Find first pizza restaurant
22+
// Finds the first restaurant document that matches the filter
2123
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
2224
Console.WriteLine($"First pizza restaurant before replacement: {oldPizzaRestaurant.Name}");
2325

24-
// Replace one document synchronously
26+
// Replaces the document by using a helper method
2527
var syncResult = ReplaceOneRestaurant();
2628
Console.WriteLine($"Restaurants modified by replacement: {syncResult.ModifiedCount}");
2729

@@ -36,13 +38,15 @@ public static void Main(string[] args)
3638
private static ReplaceOneResult ReplaceOneRestaurant()
3739
{
3840
// start-replace-one
41+
// Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza"
3942
var filter = Builders<Restaurant>.Filter
4043
.Eq(r => r.Cuisine, "Pizza");
4144

42-
// Find ID of first pizza restaurant
45+
// Finds the ID of the first restaurant document that matches the filter
4346
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
4447
var oldId = oldPizzaRestaurant.Id;
4548

49+
// Generates a new restaurant document
4650
Restaurant newPizzaRestaurant = new()
4751
{
4852
Id = oldId,
@@ -56,17 +60,18 @@ private static ReplaceOneResult ReplaceOneRestaurant()
5660
Borough = "Manhattan",
5761
};
5862

63+
// Replaces the existing restaurant document with the new document
5964
return _restaurantsCollection.ReplaceOne(filter, newPizzaRestaurant);
6065
// end-replace-one
6166
}
6267

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

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

source/includes/code-examples/replace-one/ReplaceOneAsync.cs

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

16-
// Create filter
18+
// Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza"
1719
var filter = Builders<Restaurant>.Filter
1820
.Eq(r => r.Cuisine, "Pizza");
1921

20-
// Find first pizza restaurant
22+
// Finds the first restaurant document that matches the filter
2123
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
2224
Console.WriteLine($"First pizza restaurant before replacement: {oldPizzaRestaurant.Name}");
2325

24-
// Replace one document asynchronously
26+
// Asynchronously replaces the document by using a helper method
2527
var asyncResult = await ReplaceOneRestaurant();
2628
Console.WriteLine($"Restaurants modified by replacement: {asyncResult.ModifiedCount}");
2729

@@ -36,13 +38,15 @@ public static async Task Main(string[] args)
3638
private static async Task<ReplaceOneResult> ReplaceOneRestaurant()
3739
{
3840
// start-replace-one-async
41+
// Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza"
3942
var filter = Builders<Restaurant>.Filter
4043
.Eq(r => r.Cuisine, "Pizza");
4144

42-
// Find ID of first pizza restaurant
45+
// Finds the ID of the first restaurant document that matches the filter
4346
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
4447
var oldId = oldPizzaRestaurant.Id;
4548

49+
// Generates a new restaurant document
4650
Restaurant newPizzaRestaurant = new()
4751
{
4852
Id = oldId,
@@ -56,17 +60,18 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurant()
5660
Borough = "Manhattan",
5761
};
5862

63+
// Asynchronously replaces the existing restaurant document with the new document
5964
return await _restaurantsCollection.ReplaceOneAsync(filter, newPizzaRestaurant);
6065
// end-replace-one-async
6166
}
6267

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

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

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

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

0 commit comments

Comments
 (0)