Skip to content
Advertisement

What is the fastest way to update the whole document (all fields) in MongoDB?

Let’s say I want to update the whole document and override all fields, except _id. What of the three methods is the best in terms of resource consumption:

1. Set the complete document as update parameter, so all fields are passed

Example:

collection.update({_id: docId}, {$set:updateDoc});

2. Calculate a delta document between the original and the updateDoc

Example:

const originalDoc = collection.findOne(docId);
const deltaDoc = calculateDeltaFct(originalDoc, updateDoc); //get changed fields
collection.update({_id: docId}, {$set:deltaDoc});

3. Use the Mongo 3.2. replaceOne function

Example:

collection.replaceOne({_id: docId}, {$set:updateDoc});

I have an assumption of the pros and the cons of each methods but I want to be sure what to choose and why. I am not sure how to measure it precisely, so maybe someone can help.

Background:

I have a metrics collection where many documents are updated often, but the fields to be updated vary a lot, so it is hard to write an update method for each field. Instead I intend to just throw all data in and update all fields, so I keep my code clean with only one update method for all updates.

Update:

In my setup, there are no subdocuments embedded in the document structure. I also have no sharding and replication in my (dev) setup.

Furthermore I found some method (collection.explain) which I will use to research on that topic, too. Nevertheless, any help or hint is much appreciated.

Advertisement

Answer

It really depends if you require the old information before the update. If you are overwriting the info and even for one key – > value pair then I would use either update or replaceOne. The difference may be in time depending on the size of your collection (dataset). If that is of concern benchmark the difference. Personally I would lean towards replaceOne, but that is just based on experience and the collections I deal with.

For what you have explained I don’t think your second choice is either efficient or being memory savvy. It does not sound like you need such a calculation for simply updating data where there is no concern over overwriting previous information.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement