-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathReplaceOne.cs
164 lines (129 loc) · 5.32 KB
/
ReplaceOne.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Replaces the first document that matches a filter by using the C# driver
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
namespace CSharpExamples.UsageExamples.ReplaceOne;
public class ReplaceOne
{
private static IMongoCollection<Restaurant> _restaurantsCollection;
private const string MongoConnectionString = "<connection string>";
public static void Main(string[] args)
{
try
{
Setup();
// Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza"
var filter = Builders<Restaurant>.Filter
.Eq(r => r.Cuisine, "Pizza");
// Finds the first restaurant document that matches the filter
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
Console.WriteLine($"First pizza restaurant before replacement: {oldPizzaRestaurant.Name}");
// Replaces the document by using a helper method
var syncResult = ReplaceOneRestaurant();
Console.WriteLine($"Restaurants modified by replacement: {syncResult.ModifiedCount}");
var firstPizzaRestaurant = _restaurantsCollection.Find(filter).First();
Console.WriteLine($"First pizza restaurant after replacement: {firstPizzaRestaurant.Name}");
Console.WriteLine("Resetting sample data...");
_restaurantsCollection.ReplaceOneAsync(filter, oldPizzaRestaurant);
Console.WriteLine("done.");
// Prints a message if any exceptions occur during the operation
}
catch (MongoException me)
{
Console.WriteLine("Unable to replace due to an error: " + me);
}
}
private static ReplaceOneResult ReplaceOneRestaurant()
{
// start-replace-one
// Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza"
var filter = Builders<Restaurant>.Filter
.Eq(r => r.Cuisine, "Pizza");
// Finds the ID of the first restaurant document that matches the filter
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
var oldId = oldPizzaRestaurant.Id;
// Generates a new restaurant document
Restaurant newPizzaRestaurant = new()
{
Id = oldId
Name = "Mongo's Pizza",
Cuisine = "Pizza",
Address = new Address()
{
Street = "Pizza St",
ZipCode = "10003"
},
Borough = "Manhattan",
};
// Replaces the existing restaurant document with the new document
return _restaurantsCollection.ReplaceOne(filter, newPizzaRestaurant);
// end-replace-one
}
private static ReplaceOneResult ReplaceOneRestaurantWithOptions()
{
// start-replace-one-sync-with-options
// Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza"
var filter = Builders<Restaurant>.Filter
.Eq(r => r.Cuisine, "Pizza");
// Finds the ID of the first restaurant document that matches the filter
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
var oldId = oldPizzaRestaurant.Id;
// Generates a new restaurant document
Restaurant newPizzaRestaurant = new()
{
Id = oldId
Name = "Mongo's Pizza",
Cuisine = "Pizza",
Address = new Address()
{
Street = "Pizza St",
ZipCode = "10003"
},
Borough = "Manhattan",
};
var options = new ReplaceOptions
{
BypassDocumentValidation = true
};
// Replaces the existing restaurant document with the new document
return _restaurantsCollection.ReplaceOne(filter, newPizzaRestaurant, options);
// end-replace-one-sync-with-options
}
private static void Setup()
{
// Allows automapping of the camelCase database fields to models
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
// Establishes the connection to MongoDB and accesses the restaurants database
var mongoClient = new MongoClient(MongoConnectionString);
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
}
}
public class Restaurant
{
public ObjectId Id { get; set; }
public string Name { get; set; }
[BsonElement("restaurant_id")]
public string RestaurantId { get; set; }
public string Cuisine { get; set; }
public Address Address { get; set; }
public string Borough { get; set; }
public List<GradeEntry> Grades { get; set; }
}
public class Address
{
public string Building { get; set; }
[BsonElement("coord")]
public double[] Coordinates { get; set; }
public string Street { get; set; }
[BsonElement("zipcode")]
public string ZipCode { get; set; }
}
public class GradeEntry
{
public DateTime Date { get; set; }
public string Grade { get; set; }
public float? Score { get; set; }
}