title: How does one properly increment many dates in mongoDB?
body: <p>Not being a particularly strong Javascript guy, I'm having a bit of trouble trying to update a lot of <code>Date</code> objects in Mongo.</p>
  
  <p>It seems that <a href="https://jira.mongodb.org/browse/SERVER-3174" rel="noreferrer"><code>$inc</code> has not yet been implemented for <code>Date</code> objects</a>.  So, to try and bump a bunch of dates by a day, I called (something like) this script from bash via <code>mongo myScript.js</code>:</p>
  
  <pre><code>conn = new Mongo();
  db   = conn.getDB('myDatabase');
  
  var incrementDates = function() {
    db.blah.find(myQuery).forEach(function(doc) {
  
      db.blah.update(
         { _id     : doc._id
         , my_date : { $exists : true }
         }
       , { $set : { my_date : new Date(doc.my_date.getTime() + 86400000) }}
      );
  
    });
  }
  
  incrementDates();
  </code></pre>
  
  <p>The basic idea seems to work well enough in the mongoDB shell:</p>
  
  <pre><code>&gt; var doc = db.blah.findOne(myQuery)
  &gt; doc.my_date
  ISODate("1962-11-02T23:00:00Z")
  &gt; new Date(doc.my_date.getTime() + 86400000);
  ISODate("1962-11-03T23:00:00Z")
  </code></pre>
  
  <p>But not so well in the script:</p>
  
  <pre><code>TypeError: doc.my_date has no properties
  </code></pre>
  
  <p>So I take it that I'm trying to call <code>getTime</code> on a <code>null</code> somewhere, even though the query in my update should only return documents where <code>my_date</code> exists.</p>
  
  <p>Any ideas as to what's happening here?  More importantly: is there a better way to do this?</p>