Removing element from embedded document from MongoDB using PHP?

Viewed 36

I am trying to remove two elements from one embedded document of MongoDB using PHP, but it is not working at all, though I have tested the connection with findOne and it is working fine. but not working with update/removing the element. with '$pull' operator. I need to remove Keyid and clientid

error - Call to undefined method MongoDB\Collection::update()

As Suggested I have change update to updateOne

Query:

 $items = $collection->updateOne(
['articleid' => 'd71c3fc3-38ac-11ed-9e97-54ee75046547','clientid' => 'A0010'],
['$pull'=>
    ['keyword' =>[
     'keyid'=>'94214'
        ]
         ],['clientid'=> 'A0010'] 
 ]
);

Document structure -

    {
  "_id": {
    "$oid": "62cf622c206a6fb8f9371cc6"
  },
  "articleid": "c5a58220-0309-11ed-bcd5-8c1645bdc344",
  "headline": "Bombay HC upholds order against Sebamed",
  "circulation": "6454",
  "clientid": "A0010",
  "keyword": [
    {
      "keyid": "70080",
      "keytpe": "My Company Keyword",
      "pscore": "10"
   
    },
    {
      "keyid": "70558",
      "keytpe": "My Competitor Keyword",
      "pscore": "0"
   
    }
]
} 
1 Answers

The method you are looking for is updateOne (or updateMany if you want to update multiple documents).

Related