php - MongoDB+Doctrine ODM How to remove a embedded document in a document collection? -
im using mongodb doctrine orm , want remove embedded document "avatar" (or set null) in document collection.
my json-object looks this:
{ "_id" : objectid("55965d090203ff700f000032"), "name" : "test", "stores" : [ { "_id" : objectid("559d166f0203ff081300002f"), "storename" : "test", "openingtimes" : "", "address" : { ... }, "contactperson" : { "firstname" : "", "lastname" : "", ... "avatar" : { "source" : "/uploads/images/company/55965d0980585/contactperson/", "name" : "contactperson.jpg" } } }, ... ] } im using querybuilder , try this
$company = $this->getquerybuilder() ->findandupdate() ->field('stores')->equals($store) ->field('contactperson.avatar')->set(null) ->getquery(array('multiple' => true)) ->execute(); but does´t work. how can access avatar key?
you can use equivalent of $unset in core operators example. suggest brevity , safety use:
$company = this->getquerybuilder() ->findandupdate() ->field('stores._id')->equals($storeid) ->field('stores.$.contactperson.avatar')->unsetfield()->exists(true) ->getquery() ->execute(); where hold on _id value element in "stores" array want , helps match position of array element need remove document to.
also using "dot notation" embedded document path.
that "removes" "key" document rather setting null.
if want "mutiple" want .update() instead, not return object.
ughh!
to select things use:
$company = this->getquerybuilder() ->find() ->field('stores._id')->equals($storeid) ->select('stores.$.contactperson.avatar')- ->getquery() ->execute();
Comments
Post a Comment