如何使用 Mongodb 中的时区设置进行查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14496489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do query with timezone settings in Mongodb
提问by Hyman
I'm trying to transfer my application database from Mysql to Mongodb, but I met a problem with datetime queries.
我正在尝试将我的应用程序数据库从 Mysql 转移到 Mongodb,但我遇到了日期时间查询的问题。
In Mysql, when there is a datetime or timestamp column, you can set specific timezone for each request using command:
在 Mysql 中,当有日期时间或时间戳列时,您可以使用以下命令为每个请求设置特定的时区:
SET time_zone = timezone;
// do queries here
Is there any kind of similar solutions for this kind of demands?
对于这种需求,是否有类似的解决方案?
I know applications can do the job after retrieving data from mongodb, but what about using aggregation with $hour, $month or $day operators?
我知道应用程序可以在从 mongodb 检索数据后完成这项工作,但是如何将聚合与 $hour、$month 或 $day 运算符一起使用?
回答by Nick
MongoDB stores all dates and times in UTC. This allows the timezone support and translation to be done application side instead of server side.
MongoDB 以 UTC 格式存储所有日期和时间。这允许时区支持和转换在应用程序端而不是服务器端完成。
The best way to store dates and times is via the Javascript Dateobject. This will allow you to convert dates to and from Unix timestamps, using getTime() and Date(milliseconds).
存储日期和时间的最佳方式是通过 Javascript Date对象。这将允许您使用 getTime() 和 Date(milliseconds) 将日期与 Unix 时间戳相互转换。
It should be noted, that Javascript stores time in milliseconds since UNIX Epoch, whilst the standard UNIX timestampis in seconds since Epoch.
应该注意的是,Javascript 以 UNIX Epoch 以来的毫秒为单位存储时间,而标准的UNIX 时间戳以 Epoch 以来的秒为单位。
回答by saad arshad
had a similar issue discussed here, https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/PodDGnWM09Q
在这里讨论了类似的问题, https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/PodDGnWM09Q
also, this website also might come in handy for future refrencing :) http://www.querymongo.com/
此外,这个网站也可能会派上用场以供将来参考:) http://www.querymongo.com/