Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP cannot find the class MongoClient

ErrorClass 'MongoClient' not found

Short answer

MongoClient came from the legacy mongo extension, which MongoDB declared end-of-life. Current PHP uses the mongodb extension together with the mongodb/mongodb Composer library, and the class is MongoDB\Client. Install both, load the Composer autoloader, and change the code that builds the client.

  • Applies to PHP 8.3 and 8.4, mongodb extension 2.x, mongodb/mongodb library, Composer 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which MongoDB classes your project uses and whether Composer's MongoDB library is installed. A MongoClient call means the code was written for the legacy extension.

    grep -rn "MongoClient" --include=*.php . | head
    composer show mongodb/mongodb
  2. 2Match what you see, then jump to its fix

    1. 1The project calls new MongoClient(...), and composer show finds no mongodb/mongodb packageThe code was written for the legacy mongo extensionFix
    2. 2composer show lists mongodb/mongodb, but the script that fails never requires vendor/autoload.phpThe library is installed but the Composer autoloader is not includedFix
    3. 3The detect script prints that the extension is missing, or it passes on the command line while the web server failsThe mongodb extension is not loaded in the PHP that runs the appFix

What does the error look like?

  • Class 'MongoClient' not found
  • Fatal error: Class 'MongoClient' not found

Cause 1 of 3

The code was written for the legacy mongo extension

1Check

The legacy driver's repository says it is end-of-life, with no further development, bug fixes or maintenance, and tells users to migrate to the new driver, ext-mongodb. The class in the new library is MongoDB\Client.

grep -rn "new MongoClient\|new Mongo(" --include=*.php .

2Fix

Install the extension with PIE and the library with Composer, as the MongoDB PHP library's getting-started page shows, then build the client with MongoDB\Client.

pie install mongodb/mongodb-extension
composer require mongodb/mongodb

<?php
require_once __DIR__ . '/vendor/autoload.php';

// old: $client = new MongoClient("mongodb://localhost:27017");
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->beers;

3Verify

Run the detect script that ships with the library; it reports whether the extension is loaded.

php vendor/mongodb/mongodb/tools/detect-extension.php

Confirmed by mongodb.com

Did this fix it?

Cause 2 of 3

The library is installed but the Composer autoloader is not included

1Check

The library is a Composer package. Its classes are found only after the autoloader is loaded at the start of the script or in the application's bootstrap code.

grep -rn "vendor/autoload.php" --include=*.php . | head

2Fix

Require the autoloader once, before any MongoDB class is used.

<?php
require_once __DIR__ . '/vendor/autoload.php';

3Verify

Run the script again and confirm that the client is created.

php your-script.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The mongodb extension is not loaded in the PHP that runs the app

1Check

The getting-started page points to phpinfo() and php_ini_loaded_file() to see which php.ini a PHP loads. The command line and the web server can use different ones.

php vendor/mongodb/mongodb/tools/detect-extension.php
php -r 'echo php_ini_loaded_file(), PHP_EOL;'

2Fix

Install the extension with pie install mongodb/mongodb-extension, then make sure it is enabled in each php.ini that your command line and web server use. For the full steps on a missing Driver class, see the related page.

pie install mongodb/mongodb-extension

3Verify

Run the detect script again, and load a page that calls phpinfo() through the web server.

php vendor/mongodb/mongodb/tools/detect-extension.php

Confirmed by mongodb.com

Did this fix it?

How do I stop it coming back?

  • Update the extension and the library as a pair, using the versions the getting-started page shows.
  • Port every call to MongoDB\Client instead of keeping any code that still expects the old classes.
  • Run the detect script from the library in your deployment pipeline, so a missing extension stops the release and not the first request.
  • Install the extension for the PHP that your web server runs, not only for your terminal.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.