What's the best way to store birth dates in MongoDB?

I have read a lot about how to store simple dates (no time) in MongoDB, but I still cannot find the answer. Some say to store the topic like MongoDate (date + utc time), some say to store the topic as a YYYYMMDD string, and some like other funny ways. The surest way is MongoDate, but why should I store my date of birth as a date from UTC?

In addition, the date of birth "1990-05-21" is stored as "1990-05-20T23: 00: 00Z" (the day before): this date should not change depending on the time zone, but remain the same globally.

I'm still wondering why MongoDB doesn't provide a simple "date" type like all other databases do.

+3


source to share


2 answers


As mentioned in the comments, it almost depends on your ultimate goal. I would suggest you use MongoDate because it is the native date format in MongoDB and will keep things intuitive for other developers. And the native type supports a number of useful methods out of the box that you can use, for example, in your map reduction tasks.

Also, the date of birth "1990-05-21" is stored as "1990-05-20T23: 00: 00Z" (the day before)



I think this only happens because the timezone on your server is UTC + 1. You can manually set the timezone to UTC before saving to the DB and that will fix your problem.

0


source


This is an interesting question that cannot be easily answered. As @Andrey Degtyaruk already noticed all this about the purpose of this data.

In my case, I solve almost the same problem with something like this:

store time like a YYYYMMDD string

      



but a little differently. Since the timestamp field is already reserved by MongoDB ( as per BSON docs ), I convert the date to a timestamp ( you can use one of these js scripts if you're interested ) and store it in the DB as "lastModified": "Number".

I'm not sure if my method might be relevant for your date of birth case, but it still exists and you can try it.

0


source







All Articles