如何在另一个嵌入对象(已嵌入对象)中创建嵌入对象的查询?

时间:2012-10-04 10:59:49

标签: java mongodb

我在一个集合中添加了一个嵌入对象。对象显示如下

{ "_id" : ObjectId("506d3046a4e89e856eace0cc"),
    "name": "new"
    "menuList":[{
        "name":"dssdfDS",
        "des":"dsfsdf",
        "_id" : ObjectId("506d3046a4d89e856eace0cc")
    }]
}

我创建了BasicDBObject查询以将“menuList”推送到“name”:“new”,我已成功完成此操作,现在我想将menuList1附加到menuList。

{ "_id" : ObjectId("506d3046a4e89e856eace0cc"),
    "name": "new"
    "menuList":[{
        "name":"dssdfDS",
        "des":"dsfsdf",
        "_id" : ObjectId("506d3046a4d89e856eace0cc")
        "menuList1":[{
            "name":"dssdfDS",
            "des":"dsfsdf",
            "_id" : ObjectId("506d3046a4d89e856eace0cc")
        }]
    }]
}

如何通过在 menuList 中按 menuList1 来创建 BasicDBObject查询来更新 menuList

1 个答案:

答案 0 :(得分:0)

以下查询执行技巧

  BasicDBObject query= new BasicDBObject("_id", ObjectId("506d3046a4e89e856eace0cc"));
  query.append("menuList._id", ObjectId("506d3046a4d89e856eace0cc"));

  BasicDBObject update = new BasicDBObject();
  update.append("menuList.$.menuList1", menuList1Objects);

  BasicDBObject updateCommand = new BasicDBObject("$push", update);

  db.collection.update(query,updateCommand);