Replace
ReplaceOne
IMongoCollection<T>
.ReplaceOne(FilterDefinition<T> filter, T document)var collection = database
.GetCollection<User>(Constants.UsersCollection);
// this is the new document - demo only
var newUser = RandomData.GenerateUsers(1).First();
// replace the first document in the collection
var replaceOneResult = await collection
.ReplaceOneAsync(Builders<User>.Filter.Empty, newUser);var bsonCollection = database
.GetCollection<BsonDocument>(Constants.UsersCollection);
// this is the new document - demo only
var newUser = RandomData.GenerateUsers(1).First();
var bsonReplaceOneResult = await bsonCollection
.ReplaceOneAsync(new BsonDocument(),
newUser.ToBsonDocument());db.users.replaceOne({},
{
"gender" : 1,
"firstName" : "Chris",
"lastName" : "Sakellarios",
"userName" : "Elsie.VonRueden72",
"avatar" : "https://s3.amazonaws.com/uifaces/faces/twitter/miguelmendes/128.jpg",
"email" : "Elsie.VonRueden@yahoo.com",
"dateOfBirth" : ISODate("1965-08-26T15:55:31.907+02:00"),
"address" : {
"street" : "8902 Baumbach Burg",
"suite" : "Apt. 717",
"city" : "West Carlieton",
"state" : "South Carolina",
"zipCode" : "85642-3703",
"geo" : {
"lat" : -69.6681,
"lng" : -116.3583
}
},
"phone" : "(222) 443-5341 x35825",
"website" : "https://github.com/chsakell",
"company" : {
"name" : "Abshire Inc",
"catchPhrase" : "Function-based mission-critical budgetary management",
"bs" : "monetize holistic eyeballs"
},
"salary" : 2482,
"monthlyExpenses" : 2959,
"favoriteSports" : [
"Basketball",
"Baseball",
"Table Tennis",
"Ice Hockey",
"Handball",
"Formula 1",
"American Football"
],
"profession" : "Model"
})
---------------------------
// sample update result
{
"acknowledged" : true,
"matchedCount" : 1,
"modifiedCount" : 1
}Upsert
FindOneAndReplaceOne
Last updated