MongoDB: Fatal error: Class 'MongoClient' not found
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 MongoDB\Driver\Manager
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
MongoDB\Driver\Manager 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 MongoDB\Driver\Manager("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:
- MongoDB\Driver\Manager
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 \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
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.
How to solve Fatal error: Class 'MongoClient' not found?
MongoDB\Driver\Manager is responsible for maintaining connections to MongoDB.
Connection
$mng = new MongoDB\Driver\Manager("mongodb://localhost:27017");
Listing databases
$listdatabases = new MongoDB\Driver\Command(["listDatabases" => 1]);
$res = $mng->executeCommand("admin", $listdatabases);
Reading All data
$query = new MongoDB\Driver\Query([]);
$rows = $mng->executeQuery("database_name.collection_name", $query);
foreach ($rows as $row) {
echo "$row->name\n";
}
Bulk Write(to perform two or more operations simultanously)
$bulk = new MongoDB\Driver\BulkWrite;
$doc = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'Toyota', 'price' => 26700];
$bulk->insert($doc);
$bulk->update(['name' => 'Audi'], ['$set' => ['price' => 52000]]);
$bulk->delete(['name' => 'Hummer']);
$mng->executeBulkWrite('testdb.cars', $bulk);
Class 'MongoClient' not found in file Controller\AppController.php
php_mongodb-1.4.0-7.2-ts-vc15-x64
is the "new" or rather the only officially supported driver https://pecl.php.net/package/mongodb. Its usage is documented here https://www.php.net/manual/en/set.mongodb.php and is basically uses MongoDB\Driver\Manager
.
The MongoClient
is from legacy and deprecated long time ago driver: https://pecl.php.net/package/mongo. It is documented here https://www.php.net/manual/en/book.mongo.php
There is no official support of the legacy driver for PHP 7: https://jira.mongodb.org/browse/PHP-1474
Last time the legacy driver was listed on https://docs.mongodb.com/ecosystem/drivers/php/ back in February 2018. By then it was claimed to be compatible with mongodb server v2.4, v2.6, and v3.0 with no compatibility with databases v3.2+.
The same February 2018 mongodb v3.0 retired. Support of Mongodb v3.2 has been dropped later in 2018 and v3.4 is counting its last months of support till January 2020.
Long story short, no you don't actually want this using MongoClient
.
Got error 'PHP message: PHP Fatal error: Uncaught Error: Class 'MongoClient' not found in /var/www/html/dbtest.php:5
The solution was to restart php-fpm
.
In my case I have PHP version 7.3 installed, so I needed to run this command:sudo service php7.3-fpm restart
Related Topics
Download Files from Server PHP
Failed to Open Stream: Http Wrapper Does Not Support Writeable Connections
How to Check If $_Get Is Empty
Ssl Error Ssl3_Get_Server_Certificate:Certificate Verify Failed
Codeigniter: Load Controller Within Controller
PHP Substr() Function with Utf-8 Leaves � Marks at the End
A Yaml File Cannot Contain Tabs as Indentation
Regex & PHP - Isolate Src Attribute from Img Tag
How to Assign PHP Variable Value to JavaScript Variable
Nginx Redirect Loop, Remove Index.PHP from Url
PHP Preg_Match to Find Whole Words
Return the Portion of a String Before the First Occurrence of a Character in PHP
PHP Warning Permission Denied (13) on Session_Start()