首页 > 解决方案 > 尝试使用 PHP 在 MongoDB 中执行查询

问题描述

我只是想配置 PHP 访问我的 MongoDB 数据库。首先,MongoDB 驱动程序安装在 PHP 中,并更新了 php.ini。下面的代码没有任何警告或错误,显然一切都很好:

$options =  [
    'username' => 'user',
    'password' => '123456',
    'authSource' => 'database',
];

$mongo = new MongoDB\Driver\Manager('mongodb://localhost:27017', $options);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);

$document = array("key" => "value", "another_key" => "another_value"); //...and so on...

$bulk->insert($document);

$result = $mongo->executeBulkWrite('db.mycollection', $bulk, $writeConcern);

但是,当我检查我的数据库时,什么也没发生:没有插入任何文档!!!我做错了什么???

标签: phpmongodb

解决方案


推荐阅读