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

Parse Mongo update JSON string to DBObject

我试图在运行时构造 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"}}

如果我出错了,谁能帮我解决问题,或者建议解决问题的方法?

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

Property name should be unique inside an object.

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