如何在MongoDb中保存时间戳类型值爪哇

编程入门 行业动态 更新时间:2024-10-25 13:13:36
本文介绍了如何在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 Date和ISODate格式.

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中保存时间戳类型值爪哇

本文发布于:2023-10-17 10:49:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:爪哇   类型   时间   如何在   MongoDb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!