Py学习  »  MongoDB

php mongodb异常:未授权db执行命令{update:

Roman • 5 年前 • 1675 次点击  

我有以下代码

$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:

怎么了?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39764
 
1675 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Roman
Reply   •   1 楼
Roman    6 年前

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

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