將 (.net) BsonDocument 字符串轉換為 (java) DBObject (Converting (.net) BsonDocument string into a (java) DBObject)


問題描述

將 (.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 LinharesJim Dagg)

參考文件

  1. Converting (.net) BsonDocument string into a (java) DBObject (CC BY‑SA 3.0/4.0)

#java #mongoDB






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論