mongodb: remove subdocument with where clause that includes document & subdocument -


here collection (workers):

"type" : "manager", "employees" : [{     "name" : "bob"     "id" : 101     },{     "name" : "phil"     "id" : 102     },{     "name" : "bob"     "id" : 103     }] 

first: not array $pullall not work or other array commands. want is: (1) search collection id 101 in subdocuments with type manager. (2) if 101 exists in "manager" subdocument, want remove item 103.

i have been pouring on interwebs 2 days on issue , cannot figure out.

i've tried (and many other variations):

db.workers.update( {"type":"manager","employees.id":101},{$pull : {"employees.id" : {"id" : 103}}},false,true) 

the syntax of $pull object off. try instead:

db.workers.update({"type":"manager","employees.id":101},     {$pull : {"employees" : {"id" : 103}}},false,true) 

to confirm removed:

db.workers.find({     type: "manager",      $and: [{'employees.id': 101}, {'employees.id': 103}] }) 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -