Not being a particularly strong Javascript guy, I have a bit of trouble trying to update many Date objects in Mongo.
It seems that $inc is not yet implemented for Date objects . So, to try and pounce on a bunch of dates for a day, I called (something like this) a script from bash via mongo myScript.js :
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();
The main idea seems to work quite well in the mongoDB shell:
> var doc = db.blah.findOne(myQuery) > doc.my_date ISODate("1962-11-02T23:00:00Z") > new Date(doc.my_date.getTime() + 86400000); ISODate("1962-11-03T23:00:00Z")
But not so good in the script:
TypeError: doc.my_date has no properties
So, I believe that I am trying to call getTime on null somewhere, even if the request in my update should only return documents where my_date exists.
Any ideas on what's going on here? More importantly: is there a better way to do this?
source share