How to deal with the timezone issue when storing dates in utc using mongod?

前端 未结 4 914
予麋鹿
予麋鹿 2020-12-01 01:57

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 becaus

4条回答
  •  甜味超标
    2020-12-01 02:43

    What you're asking for is currently being discussed in MongoDB issue SERVER-6310.

    I found this in a link from a discussion thread.

    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.

    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.

    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.

    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: , timezone: } that allows you to specify a timezone to use when aggregating. See here for another example in the Mongo docs.

提交回复
热议问题