简体   繁体   English

使用Java在MongoDB中插入日期

[英]Inserting Date in MongoDB using Java

I have a REST service in Dropwizard which stores data from client to MongoDB and queries MongoDB for realtime analysis on Dashboard. 我在Dropwizard中有一个REST服务,该服务将数据从客户端存储到MongoDB,并查询MongoDB以在仪表板上进行实时分析。

From client side i get JSON of Key-Value pair, in which i have a date string as follow : 从客户端获取键值对的JSON,其中有一个日期字符串,如下所示:

2014-07-14T05:58:20

and timezone as follow : 和时区如下:

"Asia/Kolkata"

I have to keep all document in mongodb in UTC. 我必须将所有文档保留在UTC的mongodb中。

I want to convert the dates to UTC according to their timezone and store. 我想根据日期和时区将日期转换为UTC。

I am using JodaDate to do so 我正在使用JodaDate这样做

DateTimeFormatter formatterInstance = DateTimeFormat
                        .forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(
                                DateTimeZone.forID(timeZone));
                dateInstance = formatterInstance.parseDateTime(timeString);
DateTime dtTz = dateInstance.withZone(DateTimeZone.UTC);

but when i try to save DateTime in mongoDB it gives following error 但是当我尝试将DateTime保存在mongoDB中时,出现以下错误

Exception in thread "main" java.lang.IllegalArgumentException: can't serialize class org.joda.time.DateTime
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:270)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:174)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:120)
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
at com.mongodb.OutMessage.putObject(OutMessage.java:289)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:261)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at com.datedemo.main.TimeZoneManager.getUTCTime(TimeZoneManager.java:65)
at com.datedemo.main.DateExample.main(DateExample.java:22)

Uptill now i was storing dates in Milliseconds DateTime.getMillis() in NumberLong() but for some queries like aggregation on day, month for LineChart display its difficult to deal with longNumber. 到目前为止,我将日期存储在NumberLong()中的毫秒DateTime.getMillis()中,但对于某些查询(如日,月,月,月,日,月,日)的汇总显示,很难处理longNumber。

Please suggest any alternate ways so that all the Dates are stored in UTC as per their Timezone. 请提出其他替代方法,以便将所有日期按照其时区存储在UTC中。 i want to avoid any conversion of Date by the REST service local timezone. 我想避免通过REST服务本地时区对日期进行任何转换。

You should look at these 3 posts which would answer your question. 您应该查看这3个可以回答您问题的帖子。

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

Bug filed in mongodb JIRA to address aggregation timezone concerns. 在mongodb JIRA中提交的错误,用于解决聚合时区问题。

https://jira.mongodb.org/browse/SERVER-6310 https://jira.mongodb.org/browse/SERVER-6310

Setting default time zone in JODA 在JODA中设置默认时区

MongoDB, Java: Retrieve date property as UTC MongoDB,Java:将日期属性检索为UTC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM