MongoDB - what is the fastest way to update all records in a collection?


I have a collection with 9 million records. I am currently using the following script to update the entire collection:


db.mydata.find().forEach(function(data) {
  db.mydata.update({_id:data._id},{$set:{pid:(2571 - data.Y + (data.X * 2572))}});

This is run from the command line as follows:

mongo my_test simple_update.js

So all I am doing is adding a new field pid based upon a simple calculation.

Is there a faster way? This takes a significant amount of time.


Not sure if it will be any faster but you could do a multi-update. Just say update where _id

By : Gandalf

This video can help you solving your question :)
By: admin