How would you approach bulk / batch updating documents (Up to 10k docs) coupled with forEach
?
(No specific criteria to update by, used for random document selection)
I'm looking at two options:
- Collect all document
_id
in theforEach
closure into an array and afterwards update usingcollection.update({_id : {$in : idsArray}}, ...)
- Add update queries in the
forEach
closure to a bulk operation and execute once done, somewhere along the lines ofbulk.find({_id: doc.id}).updateOne({...}); bulk.execute();
I'm going to benchmark this soon, but I would like to know what's more I/O efficient and considered 'smart' with Mongo.