从集合

时间:2018-02-22 05:39:42

标签: mongodb mongodb-query mongodb-php php-mongodb

我正在尝试从集合中删除子文档。我试过下面的代码行

         public function deleteContactDetailsForItsSubDocument()
{
        $this->collection->updateOne(
                            array('_id' => new MongoDB\BSON\ObjectID($this->id)),
                               array('$pull' => 
                                       array('ContactDetails.ContactTypeId' => $this->ContactTypeId)
                                     ));
}                

它抛出错误消息"未捕获异常' MongoDB \ Driver \ Exception \ BulkWriteException' with message'不能使用(ContactDetails.ContactTypeId)的部分(ContactTypeId)来遍历元素"

收藏中的文件就像

      {
      "_id": ObjectId("5a8d47d8d2ccda11fc004d91"),
      "EmployeeNumber": "9883456787",
      "FirstName": "Sana",
      ...
      "ContactDetails": [
      {
        "ContactTypeId": "04596c6f-82e6-8f00-e3a9-1f3199894284",
        "ContactType": "Phone",
        "ContactTypeValue": "99456789756" 
     },
     {
        "ContactTypeId": "71d0152c-293f-4c6f-2360-bbdfe368eacb", 
        "ContactType": "Phone",
        "ContactTypeValue": "9894567890" 
     } 
   ] 
   ...

1 个答案:

答案 0 :(得分:1)

试试这个:

$this->collection->updateOne(
                   array('_id' => new MongoDB\BSON\ObjectID($this->id)),
                   array('$pull' => 
                       array('ContactDetails' =>
                           array('ContactTypeId' => $this->ContactTypeId)
                       )
                   )
);