title: MongoDB - what is the fastest way to update all records in a collection?
body: <p>I have a collection with 9 million records.  I am currently using the following script to update the entire collection:</p>
  
  <p><b>simple_update.js</b></p>
  
  <pre><code>db.mydata.find().forEach(function(data) {
    db.mydata.update({_id:data._id},{$set:{pid:(2571 - data.Y + (data.X * 2572))}});
  });
  </code></pre>
  
  <p>This is run from the command line as follows:</p>
  
  <pre><code>mongo my_test simple_update.js
  </code></pre>
  
  <p>So all I am doing is adding a new field <b>pid</b> based upon a simple calculation.</p>
  
  <p>Is there a faster way?   This takes a significant amount of time.</p>