解析Mongo将JSON字符串更新为DBObject

时间:2016-06-21 07:09:10

标签: java json mongodb mongo-java-driver

我正在尝试在运行时构造Mongo Query,因此将JSON string解析为DBObject

我试过如下:

String then = "{$set : {"JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "Waltham", "JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "MA", "JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "Middlesex", "JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "USA", "JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "02451", "JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "42.398708",  "JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "-71.259216", "JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "71650: Metropolitan NECTA |715: Combined NECTA", "JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "71654: NECTA Division", "JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "DV257165|MT257165", "JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "781"}}"

DBObject thenQuery = (DBObject) JSON.parse(then);

结果:

{ "$set" : { "JobDoc.DataElementsRollup.ConsolidatedONET.#text.$" : "781"}}

任何人都可以解释我是否会出错,否则建议如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您在对象内多次使用相同的属性名称,这就是您获取输出的原因。

  

Property name在对象中应该是唯一的。

您不应在查询中多次使用"JobDoc.DataElementsRollup.ConsolidatedONET.#text.$"