Why would I want to store as ISODate as opposed to simple text?
2 Answers
ISODate
in MongoDB is just a function that provides a friendly wrapper for the usual JavaScript Date constructor. When you say this in MongoDB:
ISODate('2011-11-05T18:33:25Z')
You're saying the same thing as:
new Date(1320518005000);
but a human can read an ISO 8601 date a lot easier than they can read the number of milliseconds since January 1 1970.
So, using ISODate
gives you a real Date object in the database that you can call methods on (such as getMonth
) while also being able to easily eye ball its value. If you used a string for the date, then you'd be parsing strings all the time when you had to work with your dates. An example would be doing a map/reduce to aggregate monthly data; you could parse the strings to extract the month but why bother with that when you can use a real date object that knows what months are?

- 426,620
- 70
- 833
- 800
-
that was an amazing answer. thank you for the detail. I have googled this for days and I have not gotten my head around it. – Strong Like Bull Nov 05 '11 at 18:56
According to this source:
There are so many formats available, most of them incompatible with others, that it can be a usability nightmare to choose a date representation when writing for an international, cross-cultural audience, as is the case on the web. Fortunately, there is one solution in the ISO-developed international date format.

- 32,337
- 7
- 60
- 92