使用php7操作mongodb时,调用executeBulkWrite操作会抛出以下异常:
MongoDB\Driver\Exception\BulkWriteException: BulkWrite objects may only be executed once and this instance has already been executed
因为程序是常驻进程,使用的$this->bulk = new MongoDB\Driver\BulkWrite;生成的类成员变量。在使用时会多次调用$this->manager->executeBulkWrite("{$db_name}.{$db_collection}", $this->bulk);分析异常报错信息,可能是BulkWrite对象只能执行一次,于是改成$bulk = new MongoDB\Driver\BulkWrite;和$this->manager->executeBulkWrite("{$db_name}.{$db_collection}", $bulk);在每次执行executeBulkWrite时都临时生成BulkWrite对象,用临时变量进行调用,问题得到解决。
参考:https://github.com/mongodb/mongo-php-driver/commit/e0b83d83185b17069d7f5bac2a12198c73ca666c