从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中保存时间戳类型值爪哇
发布评论