If I need to update an entire document and overwrite all fields except for _id, which of the three methods is the most efficient in terms of resource consumption:
1. Setting the complete document as the update parameter, passing all fields
Example:
collection.update({_id: docId}, {$set:updateDoc});
2. Calculating a delta document between the original and updated version
Example:
const originalDoc = collection.findOne(docId);
const deltaDoc = calculateDeltaFct(originalDoc, updateDoc); //get changed fields
collection.update({_id: docId}, {$set:deltaDoc});
3. Utilizing the Mongo 3.2. replaceOne function
Example:
collection.replaceOne({_id: docId}, {$set:updateDoc});
I have some assumptions about the advantages and disadvantages of each method, but I want to make an informed decision on what to choose and why. It's difficult to measure precisely, so any help would be appreciated.
Background:
I have a metrics collection where many documents are frequently updated with varying fields, making it challenging to write specific update methods for each field. Instead, I plan to update all fields by inputting all data, simplifying my code with just one universal update method.
Update:
In my setup, there are no embedded subdocuments in the structure. Additionally, I do not have sharding or replication in my (dev) environment.
Furthermore, I have discovered a method (collection.explain) that I will use to conduct further research on this topic. Any assistance or guidance is greatly appreciated.