Question

I am communicating with a MongoDB database that is shared by another application that uses some fields that my application doesn't use or care about.

Throughout my code, I use Update.Replace to update the object. The problem is that it replaces the entire document in the database with the one from my POCO object, causing the properties used by the other application to be removed.

I could do Update.Set, but I would have to use that ALOT of times. Update.Replace is very convenient because it auto converts (by reflection) the POCO object to a BsonDocument.

How do I auto-generate an Update.Set so that I don't have to call it for every member?

Was it helpful?

Solution

Turns out I have to make an extra network call to get the whole document, then do a replace.

I take the existing document and do...

_caseCollection.Update(Query<Case>.EQ(x => x.Id, caseItem.Id), Update.Replace(existingDocument.Merge(caseItem.ToBsonDocument(), true)));

This will retain any fields that the other application is using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top