我尝试执行以下代码创建新的 mongo 连接
$m = new MongoDB\Client();
我得到了这个错误:
Fatal error: Class 'MongoDB\Client' not found
我想我已经正确安装了 MongoDB 扩展 (将 php_mongodb.dll 复制到 ext 文件夹并使用 extension=php_mongodb.dll 更新 php.ini)。
以下代码确认已加载:
echo extension_loaded("mongodb") ? "loaded\n" : "not loaded\n";
我仍然收到同样的错误。
这里是 phpinfo()
感谢您的所有帮助。谢谢!
最佳答案
如果您使用 PHP 的最新 MongoDB 扩展,MongoDB\Driver\Manager
是扩展的主要入口点。
这是使用最新扩展检索数据的示例代码。
假设您在 testDb
中有 testColl
集合。您可以使用 MongoDB\Driver\Query
检索数据扩展的类。
// Manager Class
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// Query Class
$query = new MongoDB\Driver\Query(array('age' => 30));
// Output of the executeQuery will be object of MongoDB\Driver\Cursor class
$cursor = $manager->executeQuery('testDb.testColl', $query);
// Convert cursor to Array and print result
print_r($cursor->toArray());
输出:
Array
(
[0] => stdClass Object
(
[_id] => MongoDB\BSON\ObjectID Object
(
[oid] => 5848f1394cea9483b430d5d2
)
[name] => XXXX
[age] => 30
)
)
关于php - 找不到类 'MongoDB\Client',安装了 mongodb 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40971613/