我有以下代码
$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:
出了什么问题?
答案 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);