如何在MongoDb中保存时间戳类型值爪哇 [英] How to save Timestamp type value in MongoDb | Java

查看:1090
本文介绍了如何在MongoDb中保存时间戳类型值爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Java驱动程序中,我想在MongoDb中将json下面的文件保存起来

From Java driver, I want to save a document that looks like below json in MongoDb

{ "ts" : Timestamp(1421006159, 4)}

我尝试过的选项.

选项1: Map doc = new HashMap(1);

Option 1: Map doc= new HashMap(1);

doc.put("ts", new BSONTimeStamp());

它会导致以下非必需格式

It results in the below not required format

{"ts" : {
        "_inc" : 0,
        "_class" : "org.bson.types.BSONTimestamp"
    }}

选项2:

doc.put("ts",new Timestamp(new Date().getTime()));

结果为:

{"ts" : ISODate("2015-01-12T05:36:43.343Z")}

推荐答案

我在默认的mongodb-java-driver(无spring数据)中使用了以下内容.

I used the following with the default mongodb-java-driver (no spring data).

DBObject doc= new BasicDBObject();
doc.put("ts", new BSONTimeStamp(1421006159, 4));

MongoDB的查找结果是:

And the MongoDB result for a find is:

{ "_id" : ObjectId("54b396da7fe45ee2d6c5e03a"), "ts" : Timestamp(1421006159, 4) }

因此将BSONTimeStamp序列化为类名和Class属性及其值取决于spring-data-mongodb序列化器.您应该使用默认的java-mongodb-driver或在MongoDB中使用Java DateISODate格式.

So the Serialisation of BSONTimeStamp to the classname and the Class attributes an their values depends on the spring-data-mongodb serializer. You should use the default java-mongodb-driver or use Java Date and the ISODate Format in MongoDB.

或者也许可以扩展spring-data-mongodb序列化器,并为类BSONTimeStamp编写自己的序列化器和反序列化器,以使用MongoDB Timestamp类型.

Or Maybe you could extend the spring-data-mongodb serializer and Write your own serializer and deserializer for the Class BSONTimeStamp to use the MongoDB Timestamp type.

这篇关于如何在MongoDb中保存时间戳类型值爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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