Class "MongoDB \ Client" not found, mongodb extension installed

I tried to create a new Mongo connection by running the following code

$m = new MongoDB\Client();

and I got this error:

Fatal error: class 'MongoDB \ Client' not found

I think I installed the MongoDB extension correctly (I copied php_mongodb.dll to the ext folder and updated php.ini with the extension = php_mongodb.dll).

The following code confirms that it is loaded:

 echo extension_loaded("mongodb") ? "loaded\n" : "not loaded\n"; 

I am still getting the same error.

Here is phpinfo ()

I appreciate all your help. Thanks!

+10
source share
4 answers

If you are using the latest MongoDB PHP extension, MongoDB\Driver\Manager is the main entry point to the extension.

Here is sample code to retrieve data using the latest extension.

Say you have a testColl collection in testDb . You can get data using the MongoDB\Driver\Query extension class.

 // 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()); 

Output:

 Array ( [0] => stdClass Object ( [_id] => MongoDB\BSON\ObjectID Object ( [oid] => 5848f1394cea9483b430d5d2 ) [name] => XXXX [age] => 30 ) ) 
+22
source

I am using PHP 7.1.9 and I had this problem. Solving it, uninstalling and reinstalling mongodb/mongodb

 composer remove mongodb/mongodb composer require mongodb/mongodb 

Also, if you are using Dreamweaver, do not do this to put the vendor folder in a copy of the server.

After installation, I can now use MongoDB\Client .

mongodb API version 1.3, extension Mongodb 1.4

+2
source

The same thing happened to me, check the php install version on your server. You should use php version 5.6. Check the apche error log for more accurate error information.

+1
source

Just an easy way to install

 sudo apt-get install php-mongodb 

after installing mongo

0
source

Source: https://habr.com/ru/post/1260810/


All Articles