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:
_id in the forEach closure into an array and afterwards update using
collection.update({_id : {$in : idsArray}}, ...)forEach closure to a bulk operation and execute once done, somewhere along the lines of
bulk.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.
OK, so I've benchmarked the two options.
TL;DR option one is twice as fast, so collect ids and update once.
for future reference, some more details for :
Results (times are machine specific, but the relative difference is what matters):
Looking back, I thought bulk might be faster because maybe there was some hidden optimization. But I understand that the question was missing logic, less operations probably means faster, hence bulk is slower.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With