Tom Tom - 1 year ago 485
PHP Question

PHP MongoDB: Fatal error: Class 'MongoClient' not found

When executing following PHP code:

$m = new MongoClient("mongodb://localhost:27017");

I get following error:

Fatal error: Class 'MongoClient' not found in (...)

MongoDB extension seems properly installed (I copied php_mongodb.dll to ext folder and updated php.ini).

PHP seems to confirm that extension is running properly as the following code confirms it is loaded:

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

shows that mongodb extension has been loaded.

Any hint would be greatly appreciated. Thanks! Tom

Problem still not solved.

clearly shows that the driver is loaded:
enter image description here

But I still receive the same fatal error. I have now lost a couple of days on this and will launch a bounty. Thanks for your help!

Answer Source

As the documentation is prompting it, the class is deprecated.

Warning This extension that defines this class is deprecated. Instead, the MongoDB extension should be used. Alternatives to this class include:

  • MongoDB\Driver\Manager

Source :

From what I read on their github repository release history, the class you are trying to use have been obsoleted since the version of mongodb 1.0.0, so there is an high chance that, on the version 1.6.0 you are, this is not even part of the dll.

That is also confirmed by this issue on their github

derickr commented on Apr 16

MongoClient is a class from the old legacy driver and is not supposed to be available in this one. The new driver has \MongoDB\Driver\Manager, and, the accompanying library has \MongoDB\Client.

You either need to install the old legacy extension (pecl install mongo) and use PHP 5.x, or update your code to use this new driver's classes as the old driver is not available for PHP 7. There is an upgrade guide at

Source :

Here is the replacement class documentation and the snippet of code that should replace yours :

$m = new MongoDB\Driver\Manager("mongodb://localhost:27017");

Another way, as suggested by the MongoDB member quoted here above is to use this pecl extension: instead of but please also notice the warning there stating:

This package has been superseded, but is still maintained for bugs and security fixes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download