1+ // Asynchronously replaces the first document that matches a filter by using the C# driver
2+
13using MongoDB . Bson ;
24using MongoDB . Bson . Serialization . Conventions ;
35using 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" ) ;
0 commit comments