Viewed   1k 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:

But I am still receiving the same fatal error.

 Answers

4

TL;DR

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.

Source:: https://github.com/mongodb/mongo-php-driver/releases/tag/1.0.0

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

Source: http://php.net/MongoClient


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 http://mongodb.github.io/mongo-php-library/upgrade-guide/

Source: https://github.com/mongodb/mongo-php-driver/issues/300#issuecomment-210820288


Another way, as suggested by the MongoDB member quoted here above is to use this pecl extension: https://pecl.php.net/package/mongo instead of https://pecl.php.net/package/mongodb but please also notice the warning there stating:

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

Sunday, December 11, 2022
3

Diagnose

Look up the following inside your script file

phpinfo();

If you can't find Soap Client set to enabled like so:

Fix

Do the following:

  1. Locate php.ini in your apache bin folder, I.e Apache/bin/php.ini
  2. Remove the ; from the beginning of extension=php_soap.dll
  3. Restart your Apache server
  4. Look up your phpinfo(); again and check if you see a similar picture to the one above
  5. If you do, problem solved!

On the other hand if this doesn't solve your issue, you may want to check the requirements for SOAP here. Also in the comment section you can find good advice on connecting to https.

Monday, December 5, 2022
3

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

<?php
  phpinfo();
?>

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

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

Tuesday, September 20, 2022
4

Is there a way (without manually logging into Mongo and running the command) and executing it via a PHP script without locking?

As you stated the best thing is to do it client side. As for the bandwidth, unless you got a pre-90's network then it will most likely be a very small amount of bandwidth in comparison to how much you would use for everything else including replica sets etc.

What you could do is warehouse your deletes upon their actual deletion (in your app) instead of once every day and then you would, once a day, go back through your original collection removing all deleted rows. That way the bandwidth will be spread throughout the day and when it comes to clean your production you just do a single delete command.

Another alternative would be to use an MR and make its output be that collection.

Though in general warehousing deletes in this manner is normally more work than it is worth. It is normally better to just keep them in your main collection and work your queries around the deleted flag (as you probably already do to not warehouse these immediately).

Tuesday, August 16, 2022
 
4

When you query something to MongoDB and you expect results, you will have this variable called cursor, which simply is a pointer to the document you currently did read. It is just like a scrollbar in the browser.

You can specify how many documents it should read into a buffer batchSize as you did with value 1.

It is useful when you know how much documents you expect to read. When you only need 10 documents, you can get all those in a single network packet using batchSize => 10. When specify batchSize => 5, it will take longer because it does take two network packets to the database to get the expected 10 documents.

You are safe using the default batchSize.

You can try to iterate over the cursor using foreach like in an example in the docs: http://php.net/manual/en/class.mongocommandcursor.php

Im not sure if the php.net documentation is up to date with the most current version of the MongoDB driver.

Sunday, November 13, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :