我有以下结构
{
"name": "abc",
"lname": "xyz",
"data": {
"1": {
"info": {
"test": "test"
},
"info1": {
"test": "test"
}
}
}
}
现在我想在'数据'中添加以下对象对象
"2": {
"info": {
"test": "test1"
},
"info1": {
"test": "test1"
}
}
如何使用mongodb java驱动程序在mongodb中执行此操作?
答案 0 :(得分:2)
在MongoDB shell中,您可以按照以下方式执行此操作:
db.collection.update( {_id:id} , { $set: { "data.2":
{
"info": {"test": "test1" }, "info1": {"test": "test1"}
}
}});
在Java驱动程序中:
DBObject query = new BasicDBObject("_id", "123");
DBObject update = new BasicDBObject();
DBObject info = new BasicDBObject("test","test1");
update.put("$set", new BasicDBObject("data.2",
new BasicDBObject("info",info).append("info1",info));
collection.update(query, update);