1
+ // Asynchronously replaces the first document that matches a filter by using the C# driver
2
+
1
3
using MongoDB . Bson ;
2
4
using MongoDB . Bson . Serialization . Conventions ;
3
5
using MongoDB . Driver ;
@@ -13,15 +15,15 @@ public static async Task Main(string[] args)
13
15
{
14
16
Setup ( ) ;
15
17
16
- // Create filter
18
+ // Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza"
17
19
var filter = Builders < Restaurant > . Filter
18
20
. Eq ( r => r . Cuisine , "Pizza" ) ;
19
21
20
- // Find first pizza restaurant
22
+ // Finds the first restaurant document that matches the filter
21
23
var oldPizzaRestaurant = _restaurantsCollection . Find ( filter ) . First ( ) ;
22
24
Console . WriteLine ( $ "First pizza restaurant before replacement: { oldPizzaRestaurant . Name } ") ;
23
25
24
- // Replace one document asynchronously
26
+ // Asynchronously replaces the document by using a helper method
25
27
var asyncResult = await ReplaceOneRestaurant ( ) ;
26
28
Console . WriteLine ( $ "Restaurants modified by replacement: { asyncResult . ModifiedCount } ") ;
27
29
@@ -36,13 +38,15 @@ public static async Task Main(string[] args)
36
38
private static async Task < ReplaceOneResult > ReplaceOneRestaurant ( )
37
39
{
38
40
// start-replace-one-async
41
+ // Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza"
39
42
var filter = Builders < Restaurant > . Filter
40
43
. Eq ( r => r . Cuisine , "Pizza" ) ;
41
44
42
- // Find ID of first pizza restaurant
45
+ // Finds the ID of the first restaurant document that matches the filter
43
46
var oldPizzaRestaurant = _restaurantsCollection . Find ( filter ) . First ( ) ;
44
47
var oldId = oldPizzaRestaurant . Id ;
45
48
49
+ // Generates a new restaurant document
46
50
Restaurant newPizzaRestaurant = new ( )
47
51
{
48
52
Id = oldId ,
@@ -56,17 +60,18 @@ private static async Task<ReplaceOneResult> ReplaceOneRestaurant()
56
60
Borough = "Manhattan" ,
57
61
} ;
58
62
63
+ // Asynchronously replaces the existing restaurant document with the new document
59
64
return await _restaurantsCollection . ReplaceOneAsync ( filter , newPizzaRestaurant ) ;
60
65
// end-replace-one-async
61
66
}
62
67
63
68
private static void Setup ( )
64
69
{
65
- // This allows automapping of the camelCase database fields to our models.
70
+ // Allows automapping of the camelCase database fields to models
66
71
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention ( ) } ;
67
72
ConventionRegistry . Register ( "CamelCase" , camelCaseConvention , type => true ) ;
68
73
69
- // Establish the connection to MongoDB and get the restaurants database
74
+ // Establishes the connection to MongoDB and accesses the restaurants database
70
75
var mongoClient = new MongoClient ( MongoConnectionString ) ;
71
76
var restaurantsDatabase = mongoClient . GetDatabase ( "sample_restaurants" ) ;
72
77
_restaurantsCollection = restaurantsDatabase . GetCollection < Restaurant > ( "restaurants" ) ;
0 commit comments