0

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

{ "_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

4

1 回答 1

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);
于 2012-10-04T12:02:34.043 回答