title: Update a field, depending on another field, in MongoDB documents (with Mongoose in Node.js)
body: <p>I have a collection named <code>Article</code> in MongoDB database, with the following schema and model:</p>
  
  <pre><code>var articleSchema = new Schema({
     site: String,
     date: String, 
     day: String, 
     link: {
      type: String,
      unique: true,
      index: {unique:true}    
   });
  
  var Article = mongoose.model('Article', articleSchema);
  </code></pre>
  
  <p>An example of document, which has no <code>day</code> field yet:</p>
  
  <pre><code>[ { site: 'www.atlantico.fr',
      date: '2014-05-27T11:10:19.000Z',
      link: 'http://www.atlantico.fr/example.html',
      _id: 538473817eb00f082f4803fc,
      __v: 0} ]
  </code></pre>
  
  <p>I want, for all the documents of this collection, to update the <code>day</code> field, depending on the <code>date</code> field (in a given way that I know).</p>
  
  <p>I tried that:</p>
  
  <pre><code>Article.find() // all documents
  .exec(function (err, articles) { // articles is an array
  
      for (var i=0; i&lt;articles.length; i++) { // for each document
  
          var myDate = articles[i].date; // I take its date
          myDate = myDate.replace(/^(\d{4})\-(\d{2})\-(\d{2}).*$/, '$3/$2/$1'); // I create a new variable 
  
          Article.update({date: articles[i].date}, // articles with this date
                         {day : myDate }, // update with the new variable created using the date
                         function(err, numberAffected){  
          });                           
        }
  });
  </code></pre>
  
  <p>If I run this code for a given document (by putting a condition like <code>{link: 'http://www.atlantico.fr/example.html'}</code> in <code>Article.find()</code>, it works.</p>
  
  <p>But if run the code showed above, that is supposed to do the job for all the documents, there must be a problem, because then I can see that all the documents have not been updated.</p>
  
  <p>What am I missing? Thank you for your help.</p>