【问题标题】:Call to undefined method MongoDB\Driver\Manager::executeUpdate()调用未定义的方法 MongoDB\Driver\Manager::executeUpdate()
【发布时间】:2020-06-29 11:23:06
【问题描述】:

我正在将我的 mongo 类转换为 mongoDB 管理驱动程序,当我尝试执行更新命令时出现以下错误

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);

我也试过 $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

谁能帮我解决这个问题,或者更新 MongoDB\Driver\Manager 中记录的任何其他替代方法

【问题讨论】:

    标签: php mongodb mongodb-query


    【解决方案1】:

    经过研究,我得到了解决方案

     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 
    

    对象只能执行一次,并且该实例已经执行过

    这就是答案

    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);
    }
    

    我们需要在循环中每次都初始化 BulkWrite

    【讨论】:

      猜你喜欢
      • 2016-04-20
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 2019-09-24
      • 2018-08-14
      • 2018-07-24
      • 2017-11-08
      • 1970-01-01
      相关资源
      最近更新 更多