3

Why would I want to store as ISODate as opposed to simple text?

Community
  • 1
  • 1
Strong Like Bull
  • 11,155
  • 36
  • 98
  • 169

2 Answers2

6

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?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
0

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.

DOK
  • 32,337
  • 7
  • 60
  • 92