Asked  8 Months ago    Answers:  5   Viewed   188 times

When executing the following PHP code:

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

I get the 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 the extension is running properly as the following code confirms it is loaded:

echo extension_loaded("mongodb") ? "loadedn" : "not loadedn";

Also, phpinfo() shows that mongodb extension has been loaded.

UPDATE: my problem is still not solved.

phpinfo() clearly shows that the driver is loaded:

enter image description here

But I am still receiving the same fatal error.




The class MongoClient is part of the legacy PECL package mongo but not anymore of the up-to-date mongodb package.

And since you have the mongodb extension installed, and not the mongo one, this is why you are getting the error

Fatal error: Class 'MongoClient' not found

On MongoDB PHP driver github repo, the release note about the version 1.0.0, is suggesting developers to use MongoDBDriverManager instead of MongoClient

Changes from our legacy mongo extension

Most significantly, the legacy driver's MongoClient, MongoDB, and MongoCollection classes have been obsoleted by the MongoDBDriverManager class, which is the new gateway for connecting and executing queries, commands, and write operations.


So, here is the replacement class documentation and the snippet of code that should replace yours :

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

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:

  • MongoDBDriverManager


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, on the version 1.6.0 you are, this class is not even part of the dll anymore.

That is 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 MongoDBDriverManager, and, the accompanying library has MongoDBClient.

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


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.

Wednesday, March 31, 2021
answered 8 Months ago

The problem was that I was using php -i | grep 'Configuration' to find the .ini file. This lead to /etc/php5/cli/php.ini. In retrospect, this should have been an obvious giveaway: cli means command line interface, basically for the interpreter. What I needed was the ini file that Apache was using.

Unfortunately, there was no great way to do this as I couldn't log into the www-data user, but I made a file that had the code


and that revealed that the .ini file location was actually /etc/php5/apache2/php.ini.

Once I updated that ini file with, the module was loaded at startup, so I restarted and everything is working now.

Wednesday, March 31, 2021
answered 8 Months ago

As you mentioned you're using the new Mongo extension for PHP 7.

The class names have changed from the older version, i.e.

MongoClient is now MongoDBDriverManager

MongoDate is now MongoDBBSONUTCDateTime

I'm not sure how backwards compatible everything is, but this should get you started!

Wednesday, March 31, 2021
answered 8 Months ago

Following on from the question you linked:

db.documents.find({"type": "chair"}).forEach(function(data) {   

Would be:

foreach($db->documents->find(array("type" => "chair")) as $document){
        array('_id' => $document['_id']),
            '$set' => array(
                'versions.0.content.axdducvoxb' => floatval($document['versions'][0]['content']['axdducvoxb'])

There done. good answer

Wednesday, March 31, 2021
answered 8 Months ago

You can't use a single aggregate() to do two grouped counts with your desired result format. Once the data has been grouped the first time you no longer have the details needed to create the second count.

The straightforward approach is to do two queries, as you are already doing ;-).

Thoughts on alternatives

If you really wanted to get the information in one aggregation query you could group on both fields and then do some manipulation in your application code. With two fields in the group _id, results are going to be every combination of group_name and status.

Example using the mongo shell :

    { $group: {
         _id: { group_name: "$group_name", status: "$status" },
         'total_sum': { $sum: 1 }

That doesn't seem particularly efficient and lends itself to some convoluted application code because you have to iterate the results twice to get the expected groupings.

If you only wanted the unique names for each group instead of the names + counts, you could use $addToSet in a single group.

The other obvious alternative would be to do the grouping in your application code. Do a single find() projecting only the group_name and status fields, and build up your count arrays as you iterate the results.

Saturday, May 29, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :