1

I have mongo server install and the mondo php extension enable:

enter image description here

Why am I still getting this error:

Fatal error: Class 'MongoClient' not found in ...

My code:

// connect to mongodb
$m = new MongoClient();
var_dump($m);

How I install mongo server:

`$ sudo apt-get install mongodb-server`

How I install mongo client:

`$ sudo apt-get install php-pear php5-dev`
`$ sudo pecl install mongodb`

Add extension=mongodb.so at the end of php.ini

Restart Apache sudo /etc/init.d/apache2 restart

What have I missed?

Run
  • 54,938
  • 169
  • 450
  • 748

3 Answers3

2

Try this:

Run below command in your project using terminal:

composer require mongodb/mongodb

Add below code in your php file:

require 'vendor/autoload.php';
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->db->collection;

Go through this documentation for more details.

Hope this may helpfull

1
  1. Open your Ubuntu Software Centre and search for mongo driver.
  2. Select MongoDB Database Driver php5-mongo and install it.
  3. Restart Apache: sudo /etc/init.d/apache2 restart

enter image description here

I have no idea what mongodb is installed for!!!

Run
  • 54,938
  • 169
  • 450
  • 748
-1

The MongoClient class was provided with the now deprecated mongo driver. You installed the mongodb driver, which provides the Manager class:

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
var_dump($manager);

The mongodb driver provides a minimal API for core driver functionality, but no classes such as MongoClient. The MongoDB PHP library provides a high-level abstraction around this lower-level driver, e.g. a Client class, and can be installed with composer.

code-kobold
  • 829
  • 14
  • 18
  • yet i got result from that one `object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(25) "mongodb://localhost:27017" ["cluster"]=> array(1) { [0]=> array(11) { ["host"]=> string(9) "localhost" ["port"]=> int(27017) ["type"]=> int(0) ["is_primary"]=> bool(false) ["is_secondary"]=> bool(false) ["is_arbiter"]=> bool(false) ["is_hidden"]=> bool(false) ["is_passive"]=> bool(false) ["tags"]=> array(0) { } ["last_is_master"]=> array(0) { } ["round_trip_time"]=> int(-1) } } }` – Run Jun 03 '16 at 09:14
  • but still having the same problem with `$m = new MongoClient();` – Run Jun 03 '16 at 09:15
  • `There is no class MongoClientwith the mongodbdriver;` then why do we have to install mongodb? Do u mean we have to install MongoClient via composer? – Run Jun 03 '16 at 10:40
  • The `mongo` extension, which provided `MongoClient` and other classes is deprecated; you cannot use these classes with `mongodb` driver. The replacement extension `mongodb` provides a low-level API *only*; libraries like `MongoDB PHP library` are built on top of the new `mongodb` driver and provide a convenient high level API. You can choose to build your logic using the basic low-level API coming with the `mongodb` driver you installed, or make your day easier with the help of e.g. `MongoDB PHP library`, which you can install with Composer. – code-kobold Jun 03 '16 at 11:00