jeka5555
jeka5555

Reputation: 189

mongodb-php update $pull

There is a collection "emailDeliveryActive":

{
 "_id": ObjectId("4f1950f0e902edfc3e000001"),
 "coupons": {
   "4f1950b7e902edf23e000001": {
     "_id": ObjectId("4f1950b7e902edf23e000001"),
     "couponID": ObjectId("4f15c7d8e902edb667000000")
   },
   "4f1950bfe902ed843f000000": {
     "_id": ObjectId("4f1950bfe902ed843f000000"),
     "couponID": ObjectId("4f171f33e902ed4f4f000002")
   }
 },
 "recipients": [
   {
     "email": "[email protected]",
     "get": "?auth=ZG1pdHJ5LnZvbG9zbmloaW5AZ21haWwuY29tfDA5OGY2YmNkNDYyMWQzNzNjYWRlNGU4MzI2MjdiNGY2"
   },
   {
     "email": "[email protected]",
     "get": "?auth=ZGpyb3VibGVAZ21haWwuY29tfDA5OGY2YmNkNDYyMWQzNzNjYWRlNGU4MzI2MjdiNGY2"
   },
   {
     "email": "[email protected]",
     "get": "?auth=a2FsaWJyb3YxQGdtYWlsLmNvbXwwOThmNmJjZDQ2MjFkMzczY2FkZTRlODMyNjI3YjRmNg=="
   }
 ],
 "title": "test"
}

Must be from an array of "recipients" to remove an item from a given email. Doing so:

$result = $mongoDB->emailDeliveryActive->update(
array('_id' => $emailDelivery['_id']),
array(
'$pull'=>array(
'recipients.$.email' => '[email protected]'
)
)
);

$result in getting TRUE, but the collection does not change. I'm doing something wrong?

Upvotes: 3

Views: 3623

Answers (1)

Ninja
Ninja

Reputation: 5142

You don't need the $ operator- it is for the "position of the matched array item in the query"- doesn't make sense to use here. Try as:

 $result = $mongoDB->emailDeliveryActive->update(
     array('_id' => $emailDelivery['_id']),
     array(
          '$pull'=> array('recipients' => array('email' => '[email protected]'))
          )
 );

Upvotes: 4

Related Questions