Class 'MongoClient' not found
You have not installed MongoDB PHP driver please see this link http://www.php.net/manual/en/mongo.installation.php
Update sources
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.listsudo apt-get update
Install MongoDB PHP Driver
sudo apt-get install php5-dev php5-cli php-pear -ysudo pecl install mongo
Open your php.ini file and add to it:
extension=mongo.so
Restart apache
sudo /etc/init.d/apache2 restart
Other helping info:
this should help to find your php.ini file:
php -i | grep 'Configuration File'
On Ubuntu it shows this:
Configuration File (php.ini) Path => /etc/php5/cliLoaded Configuration File => /etc/php5/cli/php.ini
take a note, that you run this command from cli (command line) so for your true php.ini go to folder apache2 instead of cli :)
For those arriving on this page with PHP 7 installed:
The MongoCLient
class was provided by pecl install mongo
.But pecl/mongo
is not available for php7 and deprecated in favor of pecl/mongodb
. But with pecl/mongodb
you'll need to use MongoDB\Driver\Manager instead of MongoClient (warning on page says so too).
See here for further reading.
This said, you will need an abstraction layer on top of the PHP MongoDB\Driver\Manager
. This is provided by mongodb/mongo-php-library.
You will need to refactor stuff like:
\MongoClient
to\MongoDB\Client
\MongoCollection
to\MongoDB\Collection
\MongoClient->selectDB
to\MongoDB\Client->selectDatabase
\MongoClient->listDBs
to\MongoDB\Client->listDatabases
- also output is not an array but an iterator, so you'll need to use
iterator_to_array
, along with edits to how you use the resulting object
- also output is not an array but an iterator, so you'll need to use
\MongoCollection->getName
to\MongoDB\Collection->getCollectionName
\MongoCollection->update
to\MongoDB\Collection->updateOne
orupdateMany
\MongoCollection->remove
to\MongoDB\Collection->deleteOne
\MongoCollection->batchInsert
to\MongoDB\Collection->insertMany
The answer is indeed to follow the instructions.I was missing the very important require line that must come before creating the new mongodb object:
<?phprequire 'vendor/autoload.php';$client = new MongoDB\Client("mongodb://localhost:27017");
And of course you need to run this command in the root of your project as per the instructions:
composer require mongodb/mongodb