在 MongoDB 中存储出生日期的最佳方法是什么? [英] What is the best way to store dates of birth in MongoDB?

查看:49
本文介绍了在 MongoDB 中存储出生日期的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于如何在 MongoDB 中存储简单日期(没有时间)的内容,但我仍然找不到答案.有人说像 MongoDate (date + utc time) 这样存储主题,有人说像 YYYYMMDD 字符串一样存储主题,还有一些像其他有趣的方式.最正确的方法好像是MongoDate,但是为什么我要把出生日期存储为UTC时间的日期?

I've read a lot about how to store simple dates (without time) in MongoDB, but I still can't find an answer. Some say to store theme like MongoDate (date + utc time), some say to store theme like a YYYYMMDD string, and some like other funny ways. The rightest way seems to be MongoDate, but why should I store a date of birth as a date with UTC time?

另外,出生日期1990-05-21"存储为1990-05-20T23:00:00Z"(前一天):这个日期不应该根据时区而改变,但保持不变全世界.

Plus, the date of birth "1990-05-21" is stored as "1990-05-20T23:00:00Z" (the day before): this date shouldn't change depending on timezone, but remain the same world wide.

我仍然想知道为什么 MongoDB 不像所有其他数据库那样提供简单的日期"类型.

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

推荐答案

简单使用:

new Date("<YYYY-mm-dd>");

它返回具有指定日期的 ISODate,没有时间戳.MongoDB 使用 ISO-8601 日期表示法来表示日期对象.这样,提供了很多日期操作.即

Which returns the ISODate with the specified date without a timestamp. MongoDB uses the ISO-8601 date notation, to represent date objects. This way, a lot of date operations are provided. I.e.

  • new Date("") 返回具有指定日期的 ISODate.

  • new Date("<YYYY-mm-dd>") returns the ISODate with the specified date.

new Date("") 指定客户端本地时区 并返回具有 UTC 中指定日期时间的 ISODate.

new Date("<YYYY-mm-ddTHH:MM:ss>") specifies the datetime in the client’s local timezone and returns the ISODate with the specified datetime in UTC.

new Date("<YYYY-mm-ddTHH:MM:ssZ>") 指定 UTC 中的日期时间并返回具有指定 UTC 中的日期时间的 ISODate.

new Date("<YYYY-mm-ddTHH:MM:ssZ>") specifies the datetime in UTC and returns the ISODate with the specified datetime in UTC.

new Date() 将日期时间指定为自 Unix 纪元(1970 年 1 月 1 日)以来的毫秒数,并返回结果 ISODate 实例.

new Date(<integer>) specifies the datetime as milliseconds since the Unix epoch (Jan 1, 1970), and returns the resulting ISODate instance.

更重要的是,在内部,日期对象存储为一个有符号的 64 位整数,表示自 Unix 纪元(1970 年 1 月 1 日)以来的毫秒数.

And even more, internally, date objects are stored as a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970).

这篇关于在 MongoDB 中存储出生日期的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆