I have an object
{
\"_id\": \"testobject\",
\"A\": \"First line\",
\"B\": \"Second line\",
\"C\": \"Third line\"
}
I want to send a RE
I suggest that you avoid relying on 1.x legacy API, as it's perfectly supported in 2.x as well, as shown in the sample code below.
var client = new MongoClient();
var database = client.GetDatabase("test");
var collection = database.GetCollection("test");
var changesJson = "{ a : 1, b : 2 }";
var changesDocument = BsonDocument.Parse(changesJson);
var filter = Builders.Filter.Eq("_id", 1);
UpdateDefinition update = null;
foreach (var change in changesDocument)
{
if (update == null)
{
var builder = Builders.Update;
update = builder.Set(change.Name, change.Value);
}
else
{
update = update.Set(change.Name, change.Value);
}
}
//following 3 lines are for debugging purposes only
//var registry = BsonSerializer.SerializerRegistry;
//var serializer = registry.GetSerializer();
//var rendered = update.Render(serializer, registry).ToJson();
//you can also use the simpler form below if you're OK with bypassing the UpdateDefinitionBuilder (and trust the JSON string to be fully correct)
update = new BsonDocumentUpdateDefinition(new BsonDocument("$set", changesDocument));
var result = collection.UpdateOne(filter, update);
Credits go to Robert Stam for providing the code sample.