vendredi 20 juillet 2018

Mongoid why should I use update_all instead of simply looping over collection and saving

I am using Mongoid in one of my rails project. I have a very big collection for which I would like to update a specific field of every document. I feel that I should use the update_all method from Mongoid :

Collection.all.update_all(field: new_value)

However in what way is it better than doing :

Collection.all.each do |document|
  document.field = new_value
  document.save
end

Can you please explain what is doing the update_all method that makes it more performant than a simple loop saving every single doc. Do you know any other way/method that could make my code more performant.

Aucun commentaire:

Enregistrer un commentaire