2

I'm trying to store and retrieve a date in mongojs. Here's how it's saved:

var game = { startedOn: new Date() };
db.games.save(game);

When I fetch it, the date is not a date anymore. It's some kind of wrapper around a date. The _d field seems to be a date, but it's weird that I should have to access it that way.

db.games.find(function(err, games){
    console.log(game[0].startedOn);
});

This logs:

{ _useUTC: true,
  _isUTC: true,
  _l: null,
  _i: null,
  _f: null,
  _d: Sun Jun 09 2013 21:49:26 GMT-0500 (CDT) }

What's the right way to store/retrieve a date in mongo-js?

bendytree
  • 13,095
  • 11
  • 75
  • 91
  • What's the `first` method that you're calling on the collection? I don't see any reference to that in the MongoJS code. – JohnnyHK Jun 10 '13 at 03:30
  • @JohnnyHK thanks, i simplified the code for brevity. I'll update it – bendytree Jun 10 '13 at 13:25
  • 1
    That looks suspiciously like a [Moment.js](http://momentjs.com/) instance, although I can't reproduce getting one back using `mongojs`. – robertklep Jun 10 '13 at 13:42
  • @robertklep it probably is - my first attempt to save dates was `moment().utc()`. I thought i deleted all that data, so maybe that's still haunting me. i'll check it out – bendytree Jun 10 '13 at 13:47
  • @robertklep yep, it was a dangling moment. thanks for the help. To anyone that cares, dates *do* properly deserialize in mongojs as you would expect them to. – bendytree Jun 11 '13 at 00:14

1 Answers1

1

DateTime and Date values should be converted to UTC Time before storing them in MongoDB

The method you have above works fine in nodejs.

Here is how you retrieve it:

new Date(parseInt(this.toString().slice(0,8), 16)*1000);

More info

Mark E. Haase
  • 25,965
  • 11
  • 66
  • 72
kushyar
  • 1,191
  • 1
  • 6
  • 10