问题描述
如果我尝试将日期字段放入文档(BSON)中并将其写入Mongo,则BSON将其以UTC格式写入.例如,日期
If I try to put a date field in a Document (BSON) and write it to Mongo, BSON writes it in UTC. For example, a date
DateTime dateTime = new DateTime("2015-07-01");
Document doc = new Document("date", dateTime.toDate());
将存储为
"date" : ISODate("2015-06-30T18:30:00Z")
在蒙哥.而且,如果我使用相同的Java驱动程序来检索它,则会得到
in Mongo. And, if I retrieve it using the same Java Driver I get it as
Wed Jul 01 00:00:00 IST 2015
太好了.有没有解决办法?我的意思是,为什么我不能按需要存储日期?如果我需要从另一个时区查询数据库怎么办?我会得到不同的结果吗?日期字段是Mongo的重要组成部分,周围包裹着许多运算符.但是,Mongo为什么不提供这种灵活性?谢谢
Great. Is there no solution to this? I mean, why can't I store my date as I want it? What If I need to query on the DB from another time zone? I will be getting different results? Date field is an important part of Mongo with a rich set of operators wrapped around it. Still, why doesn't Mongo provide this flexibility?Thanks
推荐答案
我可以通过在代码本身内将时区设置为UTC来解决问题.
I have the problem solved by setting my timezone as UTC from within the code itself.
DateTimeZone zone = DateTimeZone.UTC;
DateTimeZone.setDefault(zone);
这篇关于MongoDb BSON将日期存储在UTC时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!