6

我在集合中有以下文档:

"_id" : "2",
"workspace" : [{
        "name" : "1",
        "widgets" : [ ]
    },{
        "name" : "2",
        "widgets" : [ ]
    },{
        "name" : "3",
        "widgets" : [ ]
    },{
        "name" : "4",
        "widgets" : [ ]
    }
]}

如何{id: "1", blabla: "blabla"}在“小部件”中插入“名称”3?

4

1 回答 1

11

与仅将所有内容插入文档根目录的先前答案相比,这是使用位置运算符执行此操作的正确方法:

db.t.update({
 "_id" : "2",
 "workspace.name" : "3"
},{
 $push: {
   'workspace.$.widgets' : {
       id: "2",
       blabla: "blabla"
   }
 }
});
于 2013-10-31T22:54:39.383 回答