简体   繁体   English

将(.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. 在发布流程中,我需要使用Java将DBObject插入mongo db集合中。

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. 我将对象作为字符串接收,并且这已从使用POCO上的article.ToBsonDocument().ToJson()的.NET应用程序传递给我。

On my side of the flow, in Java, I tried just using BasicDBObject doc = (BasicDBObject) JSON.parse(content); 在流程方面,在Java中,我尝试仅使用BasicDBObject doc = (BasicDBObject) JSON.parse(content); but I get a com.mongo.util.JSONParseException on a Date: 但我在日期上收到com.mongo.util.JSONParseException

"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. 我可以更改在C#中生成内容的方式,也可以更改在Java中写入数据库的方式,我唯一的限制是必须在两个系统之间将其作为字符串传递。

Any suggestions? 有什么建议么?

EDIT Thanks to the tip from @Jim Dagg below, some googling for ISODate and BsonDocument turned out this gem . 编辑感谢下面@Jim Dagg的技巧,对ISODate和BsonDocument进行了一些搜索,结果发现了这个gem Changing the c# code to use 更改要使用的C#代码

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

fixed it. 固定它。

The ISODate constructor call is what's causing the issue. 导致问题的原因是ISODate构造函数调用。 From an issue on the MongoDB JIRA : 有关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. 解析器接受以下两种日期格式:秒-> "yyyy-MM-dd'T'HH:mm:ss'Z'"或seconds.milleseconds-> "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"只需在$date值中增加几秒钟,聚合命令就应该起作用。 Here's the JSON doc that I had success with: { "aggregate" : "test", pipeline : [ {$match : { date : { $date : "2012-05-01T12:30:00Z" } } } ] } 这是我成功使用的JSON文档: { "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. 如果删除ISODate构造函数,然后简单地将日期呈现为(例如) "2013-03-18T08:50:53Z" ,则您应该有业务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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