MongoDB - Storing date without timezone

末鹿安然 提交于 2019-12-31 01:27:28

问题


We have a simple application in which we have all user in same timezone & therefore we are not interested to store timezone information in mongo date object.

Reason for such extreme step is we have multiple micro service using common database managed by different developers. Each of them requires to explicitly set timezone related stuff in query & forgetting same results in invalid dataset.

Since currently MongoDB folks Mongo Data Types

doesn't support storing dates without timezone.

Just eager to know that is their any alternative approach to represent date without timezone in mongo by which we can still able to take advantage of mongo database queries date based syntax like date ranges, date etc.

At the same time it would be convenient for DBA's to read and manage records.


回答1:


Look at this answer: https://stackoverflow.com/a/6776273/6105830

You can use two types of long representation (milliseconds or format yyyyMMddHHmmss). These are the only ways to not store timezone and still be able to make range queries.

Unfortunately you lost some aggregation properties. But you can do something like keeping two representations and use them at opportune times.




回答2:


You could consider storing all the dates in UTC and presenting to the users in UTC, so you don't have the problem of silent conversion by either client JavaScript, server or MongoDB and therefore confusion. You can do it like this: new Date(Date.UTC(2000, 01, 28)) Here's MDN link on the subject.



来源:https://stackoverflow.com/questions/38401231/mongodb-storing-date-without-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!