mongodb-php更新$ pull

时间:2012-01-21 11:44:54

标签: php mongodb pull

有一个集合“emailDeliveryActive”:

{
 "_id": ObjectId("4f1950f0e902edfc3e000001"),
 "coupons": {
   "4f1950b7e902edf23e000001": {
     "_id": ObjectId("4f1950b7e902edf23e000001"),
     "couponID": ObjectId("4f15c7d8e902edb667000000")
   },
   "4f1950bfe902ed843f000000": {
     "_id": ObjectId("4f1950bfe902ed843f000000"),
     "couponID": ObjectId("4f171f33e902ed4f4f000002")
   }
 },
 "recipients": [
   {
     "email": "test1@gmail.com",
     "get": "?auth=ZG1pdHJ5LnZvbG9zbmloaW5AZ21haWwuY29tfDA5OGY2YmNkNDYyMWQzNzNjYWRlNGU4MzI2MjdiNGY2"
   },
   {
     "email": "test2@gmail.com",
     "get": "?auth=ZGpyb3VibGVAZ21haWwuY29tfDA5OGY2YmNkNDYyMWQzNzNjYWRlNGU4MzI2MjdiNGY2"
   },
   {
     "email": "test3@gmail.com",
     "get": "?auth=a2FsaWJyb3YxQGdtYWlsLmNvbXwwOThmNmJjZDQ2MjFkMzczY2FkZTRlODMyNjI3YjRmNg=="
   }
 ],
 "title": "test"
}

必须来自“收件人”数组才能从给定的电子邮件中删除项目。 这样做:

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

$导致获得TRUE,但集合不会改变。我做错了什么?

1 个答案:

答案 0 :(得分:3)

你不需要$运算符 - 它是“查询中匹配数组项的位置” - 在这里使用没有意义。试试:

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