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

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

问题描述

我们有一个简单的应用程序,其中所有用户都位于同一时区&因此,我们不希望将时区信息存储在mongo date对象中.

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 operatos on date fields.

MongoDB的较新版本具有用于处理时区的运算符,并且足以使用ISOTime格式.我的应用程序使用我自己的建议来存储日期.现在,我必须让我的用户选择他们的TimeZone(公司已经发展,我们需要扩展到其他国家/地区).我们正在努力使所有模型都使用时间戳而不是归一化的日期格式.

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 chance all models to use timestamp instead of a normalized date format.

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

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