insert in subdocument with mongoDB insert in subdocument with mongoDB mongodb mongodb

insert in subdocument with mongoDB


In comparison to a previous answer which just inserts everything into the root of the document, here is a correct way to do this with positional operator:

db.t.update({ "_id" : "2", "workspace.name" : "3"},{ $push: {   'workspace.$.widgets' : {       id: "2",       blabla: "blabla"   } }});