Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 304
Lê Gia Lễ Avatar asked Dec 05 '16 10:12

Lê Gia Lễ


4 Answers

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
        )

)
like image 154
dikesh Avatar answered Oct 20 '22 21:10

dikesh


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.

like image 40
Mridul Goswami Avatar answered Oct 04 '22 02:10

Mridul Goswami


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

like image 4
Rusty Avatar answered Oct 20 '22 21:10

Rusty


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.

like image 1
Prabal Thakur Avatar answered Oct 20 '22 20:10

Prabal Thakur