简体   繁体   中英

Converting (.net) BsonDocument string into a (java) DBObject

In a publishing flow I need to insert a DBObject into a mongo db collection using Java.

I receive the object as a String, and this has been passed to me from a .NET application that used article.ToBsonDocument().ToJson() on a POCO.

On my side of the flow, in Java, I tried just using BasicDBObject doc = (BasicDBObject) JSON.parse(content); but I get a com.mongo.util.JSONParseException on a Date:

"CreationDate" : ISODate("2013-03-18T08:50:53Z")

I can change how the content is generated in C#, and I can change how to write to the DB in java, my only constraint is that it must be passed as a string between the two systems.

Any suggestions?

EDIT Thanks to the tip from @Jim Dagg below, some googling for ISODate and BsonDocument turned out this gem . Changing the c# code to use

article.ToBsonDocument().ToJson(new JsonWriterSettings{OutputMode = JsonOutputMode.Strict});

fixed it.

The ISODate constructor call is what's causing the issue. From an issue on the MongoDB JIRA :

The parser accepts these two date formats: seconds -> "yyyy-MM-dd'T'HH:mm:ss'Z'" or seconds.milleseconds -> "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" Just add seconds to your $date value and the aggregation command should work. Here's the JSON doc that I had success with: { "aggregate" : "test", pipeline : [ {$match : { date : { $date : "2012-05-01T12:30:00Z" } } } ] }

If you remove the ISODate constructor and simply render your date as (for example) "2013-03-18T08:50:53Z" , you should be in business.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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