我使用Composer安装了MongoDB库:
composer require jenssegers/mongodb
我收到了这个错误:" Class' MongoDB \ Driver \ Manager'找不到",
我尝试过很多东西,但问题没有解决, 但最后我注意到类文件确实不存在于库中! 反正我还没找到......
我错过了什么?
我在哪里可以找到PHP中MongoDB的完整包?
请注意:类似的问题并未提及缺少课程档案。
答案 0 :(得分:1)
通常,您的代码应包含指向'vendor / autoload.php'的链接,并带有类似于以下内容的正确路径:
$DB_CONNECTION_STRING="mongodb://YourCredentials";
require '../../vendor/autoload.php';
然后,如果您使用MongoDB驱动程序的现代版本MongoDB \ Driver \ Manager,则您的代码中将包含诸如以下CRUD操作的内容:
创建集合中的文档:
$bulkWrite = new MongoDB\Driver\BulkWrite;
$doc=['name' => 'John', age => 33, profession => 'Guess what?'];
$bulk->insert($doc);
$mongoConn->executeBulkWrite('db.col', $bulkWrite);
阅读文档集中的文档的名称,并带有限制:
$filter = ['name' => 'John'];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$mongoConn->executeQuery('db.MyCollection', $query);
由MongoDb _id读取集合中的文档,但有以下限制:
$filter = ['_id' => new MongoDB\BSON\ObjectID( '5bdf54e6d722dc000f0aa6c2' )];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$mongoConn->executeQuery('db.MyCollection', $query);
集合中的更新文档:
$bulkWrite = new MongoDB\Driver\BulkWrite;
$filter = [];
$update = ['$set' => array()];
$options = ['multi' => false, 'upsert' => false];
$bulkWrite->update($filter, $update, $options);
$mongoConn->executeBulkWrite('db.MyCollection', $bulkWrite);
集合中的删除文档-删除:
$bulkWrite = new MongoDB\Driver\BulkWrite;
$filter = ['name' => 'John', age => 33];
$options = ['limit' => 1];
$bulkWrite->delete($filter, $options);
$mongoConn->executeBulkWrite('db.col', $bulkWrite);