我正在将我的mongo类转换为mongoDB管理驱动程序,当我尝试执行更新命令时,出现以下错误
Error:
---------------------------------
Fatal error: Uncaught Error: Call to undefined method MongoDBDriverManager::executeUpdate()
This is my code
--------------------------------
$where = array('status'=>1);
$set = array('status'=>2)
$updateOptions = array("multi" => true);
$writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 100);
$manager = new MongoDBDriverManager("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 MongoDBDriverBulkWrite();
$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 MongoDBDriverExceptionInvalidArgumentException: BulkWrite objects may only be executed once and this instance has already been executed
任何人都可以帮助我解决此问题,或者更新MongoDB\驱动程序\管理器中记录的任何其他替代方法
经过研究,我得到了解决方案
I am trying to execute $manager->executeBulkWrite on two collections same time and i got the error below
Fatal error: Uncaught MongoDBDriverExceptionInvalidArgumentException: 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 MongoDBDriverBulkWrite();
foreach($arr as $ar){
$where = array('status'=>1);
$set = array('status'=>2)
$updateOptions = array("multi" => true);
$bulk = new MongoDBDriverBulkWrite();
$bulk->update($where,$set,['multiple' => true, 'upsert' => false]);
$result = $manager->executeBulkWrite($db.'.'.$collection, $bulk);
}
我们需要每次循环初始化 BulkWrite