Mongodb implications of update versus replace

Question

I have read this related question, but the one below is different. The mongodb c# driver has a ReplaceOne method (& an async counterpart) on the document collection class, which can be used to replace the entire contents of a document that fits the filter argument. The alternative is to use the UpdateOne or UpdateMany methods (or async counterparts), which requires the building of an UpdateDefinition<TDocument>.

My question has to do with the implications of choosing one of these methods over the other (replace vs update), in cases where you have enough input data to choose either to achieve the same result. In other words, if I have the entire original document and only wish to update a small slice of its contents.

The first factor I can think of is the payload sent to the database server. Though I have not read any mongodb c# driver source and could not find any documents to verify this, it seems that ReplaceOne could have to send more bytes over with the update operation, especially for larger documents. The Update... methods seem like they could get away with a smaller payload by only sending over update metadata for the slices of the document that require modification (in addition to filter criteria, which both methods must send). Can anyone verify if this is an accurate assumption?

Another factor brought up by a colleague is that the choice of the method (update vs. replace) could affect document indexing as well. The assumption here is that using ReplaceOne has the potential to cause the database to rebuild all indexes for the document being updated, whereas the Update... methods have enough change metadata information to avoid rebuilding indexes on fields which are not part of the metadata in the update definition. Can anyone verify whether or not mongodb internally handles document index building differently depending on whether a document is modified using replace versus update?

A third factor came up for us a couple of times already, regarding the AddToSet and PullFilter methods on the Update<TDefinition> class. It seems that the Update... methods will not allow you to modify a set in a document (like a json array) by both adding items to it and removing items from it at the same time; these operations have to be sent individually, using 2 separate calls to the Update... method along with separate Update<TDefinition> instances (though with the same filter arguments). The ReplaceOne method, in this case, seems to be the only way to make this kind of document change in a single "transaction", at least when using the C# driver. We're currently using Update... over ReplaceOne for now because we're uncertain whether the alternative would negatively affect indexing as mentioned above.

Other than these, what are any additional implications that could lead one to choose from the ReplaceOne family of methods over the Update... family or vice-versa? Again, this is assuming that you have enough input data (i.e. all document data) to achieve the same result with either approach, don't mind mutating state directly (via replace), and don't mind building mongo definitions (via update).


Show source
| c#   | mongodb   | replace   | updates   2017-09-05 21:09 0 Answers

Answers to Mongodb implications of update versus replace ( 0 )

Leave a reply to - Mongodb implications of update versus replace

◀ Go back