Reputation: 53
I have MongoDB installed in my cPanel/WHM CentOS server. I have the PHP Drivers installed. I have Port 27017 opened.
This works $m = new MongoDB\Driver\Manager();
These do not work:
$m = new Mongo();
$m = new MongoClient();
Both result in Fatal error: Class 'Mongo' not found in ...
and Fatal error: Class 'MongoClient' not found in ...
.
Does anyone know why?
Not a duplicate. That SO Q is for a Windows machine. My server is Linux/CentOS
From My Hosting Provider's Sys Admin:
root@host [~/support/642192]# pecl search mongo Retrieving data...0%
Package Stable/(Latest) Local mongo 1.6.12 (stable) MongoDB database driver (legacy)
Upvotes: 1
Views: 2024
Reputation: 86
You cannot use the following classes with the new mongodb
driver:
$m = new Mongo();
$m = new MongoClient();
This corresponds to the legacy mongo
driver. Instead you should use MongoDB\Client
through the MongoDB PHP Library.
All right. I lost some hair on this story as well, because the documentation about Mongo and PHP is extremely confusing. The options are then to get mad, bald, or both. But i found courage and finally got it. So this might help you.
First of all, you must clarify which MongoDB driver you use: either mongo
(legacy) or mongodb
(new). Note the smaller case. First source of confusion, the mongo
driver is sometimes referred to as MongoDB (legacy) PHP driver.
https://docs.mongodb.com/ecosystem/drivers/php/
Second source confusion, the version numbers are not logical, as mongo
driver had 1.5, 1.6 but mongodb
starts again from 1.0. So, going forward, but backwards, it's just insane... Imo they should have started from 2.0!
The choice of the driver depends on your PHP version:
mongo
legacy driver (1.5, 1.6)mongodb
driver (1.1+)mongo
(1.5, 1.6) or the new mongodb
(1.0, 1.1+)But it's not over. The driver is just a low-level interface (aka PHP extension). Now we come to the programmer's API and it becomes even worse.
Third source of confusion, the low-level drivers and the API classes have overlapping names.
mongo
legacy driver -> classes MongoClient, MongoDB (!), ...mongodb
driver -> classes MongoDB\Driver, MongoDB\BSON, ...But it's not over yet. The old mongo
driver could be used directly. The new mongodb
driver provides classes (such as MongoDB\Driver) but it is actually a low-level API. You are not supposed to use it directly, you could, but it's not convenient. Instead you should use the MongoDB PHP Library which gives an API similar to the old MongoClient classes...!
https://docs.mongodb.com/php-library/master/
So if you installed mongodb
, you should install this MongoDB PHP Library in order to use MongoDB\Client. This class is supposed to be similar to the old MongoClient, but there are some differences such as the sort and projection.
To install this library you are advised to use the tool called Composer which allows to you download these classes into your repo. Then use the autoloader provided with Composer.
And here we come to the 4th source of confusion, MongoDB PHP Library is versioned from 1.0 even though you are using the last mongodb
driver in version 1.1 ! It's certainly obvious for those who developed this stuff but very hard to follow for lambda users. Crazy confusing stuff.
There are also many other libraries above the drivers, but i don't know them at all so i won't go more into them (https://docs.mongodb.com/ecosystem/drivers/php-libraries/).
Depending on your PHP version, clarify which MongoDB driver to use: mongo
(legacy) or mongodb
(new) ?
mongo
you can use MongoClient
class directly.mongodb
you should also install MongoDB PHP Library to use MongoDB\Client
class.Good luck! :)
Upvotes: 2
Reputation: 1540
Looks like you haven't enabled or installed php_mongo extension. After enabling it restart apache and check phpinfo() to see if its properly enabled.
Upvotes: 1
Reputation: 322
You have use the correct namespace. As stated in their docs the namespace for the client is MongoDB\Client
. Alterantivly you can use a use
statement like this use MonogoDB\Client
.
Upvotes: 0