6

I have the following document in the collection:

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

How can I insert {id: "1", blabla: "blabla"} in "widgets" for the "name" 3?

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
Ricklemer
  • 201
  • 1
  • 2
  • 10

1 Answers1

12

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"
   }
 }
});
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • As far as I'm aware this is currently not an option in Meteor's client side mini-mongo(they haven't implemented the positional operator. It's something they want to have eventually though). This will only work server side. For my apps I've resorted to doing these types of tasks server side. Which is OK but you have to duplicate your security/validation code. – Dave Nov 01 '13 at 15:54
  • they haven't hit 1.0, they're still in preview/development. – Dave Nov 01 '13 at 16:53