我知道有一个 BasicDBObject 可以让你执行:
BasicDBObject info = new BasicDBObject();
info.put("x", 203);
info.put("y", 102);
我遇到的问题是该值只能是原始类型。 我有一个 json 对象,我想将其与无法修改的通用数据一起存储,但想在单个 mongo 文档中描述 json 对象。我该怎么做才能做到这样的事情:
BasicDBObject info = new BasicDBObject();
info.put("Name", "John");
info.put("Main Hobby", "Hiking");
info.put("Albums", json-string-with-nested-arrays);
总而言之,我正在寻找一种方法,允许我在同一文档中存储 json 对象以及键值对(假设我拥有的“json-string-with-nested-arrays”是不可修改,因此我无法向其中插入其他属性。)我怎样才能完成此任务?
下面是 json-string-with-nested-arrays:
{"data":[{"stuff":[
{"onetype":[
{"id":1,"name":"John Doe"},
{"id":2,"name":"Don Joeh"}
]},
{"othertype":[
{"id":2,"company":"ACME"}
]}]
},{"otherstuff":[
{"thing":
[[1,42],[2,2]]
}]
}]}
最佳答案
如果“json-string-with-nested-arrays”是一个JSON字符串,那么你可以在mongo-java-driver中执行类似的操作。
info.put("Albums", JSON.parse(json-string-with-nested-arrays));
此 JSON.parse()
方法是 mongo-java-driver 的一部分
关于java - 是否可以使用 Mongodb java 的 json 对象填充其他字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11112574/