問題描述
將 (.net) BsonDocument 字符串轉換為 (java) DBObject (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
<pre class="lang‑cs prettyprint‑override">article.ToBsonDocument().ToJson(new JsonWriterSettings{OutputMode = JsonOutputMode.Strict});
</pre>
fixed it.
參考解法
方法 1:
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.
(by Nuno Linhares、Jim Dagg)