0

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

         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' 和消息'不能使用 (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" 
     } 
   ] 
   ...
4

1 回答 1

1

试试这个:

$this->collection->updateOne(
                   array('_id' => new MongoDB\BSON\ObjectID($this->id)),
                   array('$pull' => 
                       array('ContactDetails' =>
                           array('ContactTypeId' => $this->ContactTypeId)
                       )
                   )
);
于 2018-02-22T05:45:44.137 回答