PHP Post API is not working with MongoDB?

Viewed 22

I am trying to POST form data into a PHP script and update the MongoDB document, as I have checked data is posting on the page successfully but the MongoDB query is neither running nor giving any error! POST values are in string type and also collection values are string too.

I am using $pull to remove a value from the document using UpdateOne()

How will I pass two parameters to filter a document and then update a field of it?

In MongoDB, if we have to find a document with two parameters and update/remove a field this will be like -

db.collection.updateOne({'articleid':$articleid,'clientid':$clientid},{'$pull':{'keyword':{'keyid':'$keyid'}}})

PHP: -

$client = new MongoDB\Client(URI)
$articleid =  $_POST['Articleid'];
$clientid = $_POST['clientid'];
$keyid = $_POST['keyid'];
$putid ='';



$collection = $client->DBNAME->collection-name;

 
try {
 
   $collection->updateOne(['articleid' => $articleid,'clientid'=> $clientid],
                          ['$pull'=> ['keyword' =>[ 'keyid'=> $keyid]] ]);
 
 

}catch(MongoCursorException $e) {
    echo $e;
 
}  

Sample Document: -

    {
  "_id": {
    "$oid": "62ebe239206a6fb8f9e1fb9f"
  },
  "articleid": "e8c960af-1407-11ed-83ce-",  
  "imagedirectory": "2208image", 
  "clientid": "A0010", 
  "keyword": [
    {
    
      "keyid": "94214", 
      "companys": "null", 
    }
  ],
 
  "region": "North"
} 
0 Answers
Related