diwakar wisdom
diwakar wisdom

Reputation: 31

Call to undefined method MongoDB\Driver\Manager::executeUpdate()

I am converting my mongo class to mongoDB manage driver, When i try to execute the update command i have got the following error

Error:
---------------------------------
Fatal error: Uncaught Error: Call to undefined method MongoDB\Driver\Manager::executeUpdate()

This is my code
--------------------------------
$where  = array('status'=>1);
$set = array('status'=>2)

$updateOptions = array("multi" => true);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$result = $manager->executeUpdate("mydb.collection", $where, $set, $updateOptions, $writeConcern);

Also I tried with $manager->executeBulkWrite

Here is the code
----------------------------

$where  = array('status'=>1);
$set = array('status'=>2)
$updateOptions = array("multi" => true);
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->update($where,$set,['multiple' => true, 'upsert' => false]);
$result = $manager->executeBulkWrite($db.'.'.$collection, $bulk);

I am trying to execute  $manager->executeBulkWrite on two collections same time and i got the error below
Fatal error: Uncaught MongoDB\Driver\Exception\InvalidArgumentException: BulkWrite objects may only be executed once and this instance has already been executed

Can any one help me to solve this issue, Or any other alternative for updating the records in MongoDB\Driver\Manager

Upvotes: 0

Views: 2431

Answers (1)

diwakar wisdom
diwakar wisdom

Reputation: 31

After research I got solution for

 I am trying to execute  $manager->executeBulkWrite on two collections same time and i got the error below
 Fatal error: Uncaught MongoDB\Driver\Exception\InvalidArgumentException: BulkWrite 

objects may only be executed once and this instance has already been executed

Here is the answer

As i said before i used two update queries at once while calling a function with global variable $bulk
When we try to update the data using $manager->executeBulkWrite in a loop, We should not use the global variable for 
$bulk = new MongoDB\Driver\BulkWrite();
foreach($arr as $ar){
   $where  = array('status'=>1);
   $set = array('status'=>2)
   $updateOptions = array("multi" => true);
   $bulk = new MongoDB\Driver\BulkWrite();
   $bulk->update($where,$set,['multiple' => true, 'upsert' => false]);
   $result = $manager->executeBulkWrite($db.'.'.$collection, $bulk);
}

We need initialize the BulkWrite every time in loop

Upvotes: 1

Related Questions