php - remove elements from sub-array mongo document -


i`m trying remove subarray element mongo document. document(record) this:

{     "_id" : 1,     ...,     "team" : {         "players" : [{             "name" : "a b",             "birthday" : new date("11/11/1995")           }, {             "name" : "a c",             "birthday" : new date("4/4/1991")           }],         "matches" : [{             "against" : "team b",             "matchday" : new date("11/16/2012 10:00:00")           }]       } } 

now want remove "a b" player document. tried this:

$result = $collection->update(     array('_id' => 1),      array('$pull' => array('team.players.name' => 'a b')) ); 

the result seems ok

(     [updatedexisting] => 1     [n] => 1     [connectionid] => 8     [err] =>      [ok] => 1 ) 

but player still exists in document.

thanks!

your update object should that:

{     "$pull": {         "team.players": {             name: "a c"         }     } } 

so in php be:

$result = $collection->update(     array('_id' => 1),      array('$pull' =>          array('team.players' => array('name' = > 'a b'))     ) ); 

Comments