从 collection 中的文档中删除子文档

Deleting a sub document from a document in collection

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

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

它抛出错误信息"Uncaught exception 'MongoDB\Driver\Exception\BulkWriteException' with message 'Cannot use the part (ContactTypeId) of (ContactDetails.ContactTypeId) to traverse the element"

collection中的文档像

      {
      "_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" 
     } 
   ] 
   ...

试试这个:

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