MongoDB - 存储没有时区的日期 [英] MongoDB - Storing date without timezone

查看:29
本文介绍了MongoDB - 存储没有时区的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个简单的应用程序,其中所有用户都在同一时区 &因此我们对在 mongo 日期对象中存储时区信息不感兴趣.

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.

由于目前 MongoDB 的人们Mongo 数据类型

Since currently MongoDB folks Mongo Data Types

不支持存储没有时区的日期.

doesn't support storing dates without timezone.

只是想知道这是他们在 mongo 中表示没有时区的日期的任何替代方法,通过它我们仍然可以利用 mongo 数据库查询基于日期的语法,如日期范围、日期等.

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.

同时方便DBA阅读和管理记录.

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

推荐答案

看这个答案:https://stackoverflow.com/a/6776273/6105830

您可以使用两种类型的长表示(毫秒或格式 yyyyMMddHHmmss).这些是不存储时区但仍然能够进行范围查询的唯一方法.

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.

更新:

不要像我之前说的那样存储日期.您将失去 MongoDB 的许多功能,并且将难以在日期字段上执行主要操作.

Do not store date as I said before. You will lost many and many features of MongoDB and also will be hard to perform major operators on date fields.

较新版本的 MongoDB 具有处理时区的运算符,并且应该足以处理 ISOTime 格式.我的应用程序使用我自己的建议来存储日期.现在我必须让我的用户选择他们的时区(公司已经发展,我们需要扩展到其他国家).我们正在努力将所有模型更改为使用时间戳而不是规范化的日期格式.如需进一步了解链接:https://docs.mongodb.com/manual/参考/方法/日期/

Newer versions of MongoDB has operators to deal with timezone, and it should be enough to work with ISOTime formats. My application was using my own suggestion to store date. Now I have to let my users select their TimeZone (company has grown and we need to expand to other countries). We are struggling to change all models to use timestamp instead of a normalized date format. For further more explore the link: https://docs.mongodb.com/manual/reference/method/Date/

也可以使用MongoDB官方社区频道提问这是链接:https://developer.mongodb.com/community/forums/

and you can also use MongoDB official community channel for questioning Here is the link: https://developer.mongodb.com/community/forums/

这篇关于MongoDB - 存储没有时区的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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