I'm using the MongoDB .Net driver in my project. I want to update all of the properties of my object that is stored in MongoDB. In the documentation, update is shown like this:
var filter = Builders<BsonDocument>.Filter.Eq("i", 10);
var update = Builders<BsonDocument>.Update.Set("i", 110);
await collection.UpdateOneAsync(filter, update);
But I don't want to call the Set
method for all of the properties, since there are many properties and can be many more in the future.
How can I update the whole object using the MongoDB .Net driver?
$set
and related operators (which is all these driver builders are doing) on the fields you actually want to update. So if you just need to change one then you list one. Is that what your problem is or is it that you want to change 20 out of 50 properties in your update? – Lordinwaiting$set
. But there is a handy helper method that does this for you rather than just serializing the whole document. – Lordinwaiting