使用mongod在utc中存储日期时如何处理时区问题? [英] How to deal with the timezone issue when storing dates in utc using mongod?

查看:121
本文介绍了使用mongod在utc中存储日期时如何处理时区问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mongodb集合,其中每个文档都有一些属性和utc时间戳.我需要从集合中提取数据并使用聚合框架,因为我使用集合中的数据在用户界面上显示一些图表.但是,我需要根据用户的时区进行汇总.假设我知道用户的时区(通过浏览器的请求或其他方式传递了),是否有任何方法可以使用聚合框架基于[客户端的]时区进行聚合?

I have a mongodb collection where each document has some attributes and a utc timestamp. I need to pull out data from the collection and use the aggregation framework because I use the data from the collection to display some charts on the user interface. However, I need to do the aggregation as per the user's timezone. Assuming I know the user's timezone(passed in the request from browser or in some other manner), is there any way to use the aggregation framework to aggregate based on the [client's] timezone?

推荐答案

MongoDB问题SERVER-6310 .

我在讨论线程.

该问题对于按日期进行的任何分组都是常见的,包括SQL数据库和NoSQL数据库.实际上,我最近在RavenDB中解决了这个问题.对该问题有很好的描述,并且使用了RavenDB解决方案此处.

The problem is common for any grouping by date, including SQL databases and NoSQL databases. In fact, I recently addressed this head on in RavenDB. There is a good description of the problem and a RavenDB solution here.

MongoDB问题讨论了一种解决方法,该解决方案与我在上面的评论中描述的类似.您可以预先计算出您感兴趣的当地时间,然后根据这些时间进行分组.

The MongoDB issues discusses a workaround, which is similar to what I described in comments above. You precalculate the local times you are interested in, and group by those instead.

用这两种方法都很难覆盖世界上的每个时区.您应该选择一些对您的用户群有意义的目标区域,例如我在RavenDB文章中介绍的基于办公室的方法.

It will be difficult to cover every time zone in the world with either approach. You should decide on a small handful of target zones that make sense for your user base, such as the per-office approach I described in the RavenDB article.

更新:此问题已于2017年7月在MongoDB中解决(版本3.5.11).该解决方案在上面的第一个链接中进行了介绍,但总而言之,他们为日期引入了一种新的对象格式在聚合表达式中:{ date: <dateExpression>, timezone: <tzExpression> },它允许您指定聚合时使用的时区.有关Mongo文档中的另一个示例,请参见此处.

UPDATE: This issue was solved in MongoDB in July 2017 (version 3.5.11). The solution is described in the first link above, but in short they have introduced a new object format for dates in aggregation expressions: { date: <dateExpression>, timezone: <tzExpression> } that allows you to specify a timezone to use when aggregating. See here for another example in the Mongo docs.

这篇关于使用mongod在utc中存储日期时如何处理时区问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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