Lê Gia Lễ
Lê Gia Lễ

Reputation: 540

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

I tried to create new mongo connection executing the following code

$m = new MongoDB\Client();

and i got this error:

Fatal error: Class 'MongoDB\Client' not found

i think i have properly installed MongoDB extension (Copied php_mongodb.dll to ext folder and updated php.ini with extension=php_mongodb.dll).

The following code confirms it is loaded:

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

I still receive the same error.

Here is phpinfo()

I appreciate all your help. Thank you!

Upvotes: 15

Views: 40482

Answers (7)

user956584
user956584

Reputation: 5558

\MongoDB\Driver\Manager(); is now "low level" extension (php_mongodb), \MongoDB\Client was rewritten to "PHP class" and you need extra install https://docs.mongodb.com/drivers/php/ to use old style nice new \MongoDB\Client('mongodb://127.0.0.1');

Upvotes: 2

Mridul Goswami
Mridul Goswami

Reputation: 9

Different for Apache and Nginx it appears.

Though this post is old but I help this migh help someone. I recently ran into same problem. With me it was s different php.ini.

I kept placing the mongoextension into /cli/php.ini.

Thoough when I ran <?php phpinfo() ?> I found out that in my case the loaded configuration is in /fpm/php.ini. This was because I was unsing Nginx with fpm.

Upvotes: 0

Kipruto
Kipruto

Reputation: 857

90% of the time it is your Mongodb extension configuration. kindly check in php.ini

Upvotes: 0

Sumit Manchanda
Sumit Manchanda

Reputation: 197

Just simple way to install

sudo apt-get install php-mongodb

after install mongo

Upvotes: 0

Ikhlak S.
Ikhlak S.

Reputation: 9034

I'm using PHP 7.1.9 and I had this issue. Solved it by removing and reinstalling mongodb/mongodb

composer remove mongodb/mongodb
composer require mongodb/mongodb

Also, If you are using Dreamweaver, don't for get to put the vendor folder in the server copy.

After installing, I can now use MongoDB\Client.

mongodb API Version 1.3, Mongodb Extension 1.4

Upvotes: 4

dikesh
dikesh

Reputation: 3125

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

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

Let's say you have testColl collection in testDb. The you can retrieve data using MongoDB\Driver\Query class of the extension.

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

)

Upvotes: 42

Prabal Thakur
Prabal Thakur

Reputation: 129

same happened with me, check the version of php install on your server. You have to use php version 5.6 .Check the apche error log to get more precise error detail.

Upvotes: 0

Related Questions