Php MongoDB异常:未经数据库授权执行命令{更新:

时间:2018-11-13 04:06:18

标签: php mongodb

我有以下代码

$bulkWrite=new MongoDB\Driver\BulkWrite;
$filter = ['name' => 'John'];
$update = ['$set' => ['name' => 'Smith', age: 35, profession => 'pilot']];
$options = array('multi' => false, 'upsert' => false);
$bulkWrite->update($filter, $options);
$mongoConn->executeBulkWrite('MyCollection', $bulkWrite);

代码返回错误:

Exception:not authorized on db to execute command { update:

出了什么问题?

1 个答案:

答案 0 :(得分:0)

查看代码后,我发现问题出在集合名称中。
如果我们之前有过

$DB_CONNECTION_STRING="mongodb://dbName:dbPassword";
require '../../vendor/autoload.php';
$mongoConn = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING );

然后我们需要正确引用dbName:

$bulkWrite=new MongoDB\Driver\BulkWrite;
$filter = ['name' => 'John'];
$update = ['$set' => ['name' => 'Smith', age: 35, profession => 'pilot']];
$options = array('multi' => false, 'upsert' => false);
$bulkWrite->update($filter, $options);
$mongoConn->executeBulkWrite('dbName.MyCollection', $bulkWrite);