0

Maybe this is a dumb question for backend guys but I'm new in this field. I'm making a service using node.js with mongoDB and I'm curious if there's any drawback about using YYYYMMDDHHmmss string format for persisting dates.

I know that another way would be using unix timestamp but the issue there is that it's not easy to read at first sight.

What would you recommend?

Thanks in advance!

betzerra
  • 839
  • 1
  • 8
  • 17

1 Answers1

1

I think a third option is better, using the built in Date format. It stores dates as a 64 bit number representing the number of milliseconds since the epoch (like a Unix Epoch times 1000). However it comes with a fair amount of built in support for converting to displayable formats. Most of the drivers have good support for converting to and from that format. Also, the MongoDB aggregation framework directly supports it which greatly simplifies roll-up reporting by date, month, year, etc.

The mongo shell has great support for the built in Date format:

http://docs.mongodb.org/manual/core/shell-types/

So does the aggregation framework:

http://docs.mongodb.org/manual/reference/operator/aggregation-date/

And you can see here http://mongodb.github.io/node-mongodb-native/api-articles/nodekoarticle1.html#mongo-db-data-types that the Date format

I've personally made the mistake of storing custom date formats and then dealt with difficulties in reporting, grouping and conversions. I now always start with the built-in Date format and only deviate if something else requires it.

maps directly to a Javascript Date

John Petrone
  • 26,943
  • 6
  • 63
  • 68