I want to make a general method to do PARTIAL updates of a document from json data. The json data contiains a subset of the fields of the POCO being updated. I have this:
public static void Update(MongoCollection collection, ObjectId id, string jsonData) {
// assume jsonData does not contain the id field.
var bsonDoc = BsonSerializer.Deserialize<BsonDocument>(jsonData);
IMongoUpdate updateDoc = new UpdateDocument("$set", bsonDoc);
collection.Update(Query.EQ("_id",id), updateDoc);
}
This almost works, but my problem is that fields with type ObjectId are deserialized as strings, because that is how they are represented in JSON. (They have been serialized with the javascript JSON2 lib on the client side).
So my questions are:
1) What is the best way to make a general partial update method working with json data in with the official C# driver?
2) How can I get poco fields deserialized correctly to the proper types instead of just strings when using the BsonSerializer?
See also: https://groups.google.com/group/mongodb-user/browse_thread/thread/b2162a80550124c7#