Class 'MongoDB\Client' not found while following offical installation docs

Hey everyone, I’ve encountered a bit of a problem following along with the offical docs here.
Error: Class 'MongoDB\Client' not found in C:\xampp\htdocs\mongo.php on line 6

My code

I was unable to get the Mongo extension installed using PECL (tells me that it doesn’t exist) so I had to install it manually. I have confirmed it’s installed correctly:

php --ri mongodb
mongodb

MongoDB support => enabled
MongoDB extension version => 1.9.0
MongoDB extension stability => stable
libbson bundled version => 1.17.2
libmongoc bundled version => 1.17.2
libmongoc SSL => enabled
libmongoc SSL library => OpenSSL
libmongoc crypto => enabled
libmongoc crypto library => libcrypto
libmongoc crypto system profile => disabled
libmongoc SASL => enabled
libmongoc ICU => disabled
libmongoc compression => disabled
libmongocrypt bundled version => 1.0.4
libmongocrypt crypto => enabled
libmongocrypt crypto library => libcrypto

Directive => Local Value => Master Value
mongodb.debug => no value => no value

Next I installed the mongodb library via composer and confirmed it’s been added correctly:

composer show mongodb/mongodb
name     : mongodb/mongodb
descrip. : MongoDB driver library
keywords : database, driver, mongodb, persistence
versions : * 1.8.0
type     : library
license  : Apache License 2.0 (Apache-2.0) (OSI approved) https://spdx.org/licenses/Apache-2.0.html#licenseText
homepage : https://jira.mongodb.org/browse/PHPLIB
source   : [git] https://github.com/mongodb/mongo-php-library.git 953dbc19443aa9314c44b7217a16873347e6840d
dist     : [zip] https://api.github.com/repos/mongodb/mongo-php-library/zipball/953dbc19443aa9314c44b7217a16873347e6840d 953dbc19443aa9314c44b7217a16873347e6840d
path     : C:\git\waw\vendor\mongodb\mongodb
names    : mongodb/mongodb

support
issues : https://github.com/mongodb/mongo-php-library/issues
source : https://github.com/mongodb/mongo-php-library/tree/1.8.0

autoload
psr-4
MongoDB\ => src/
files

requires
ext-hash *
ext-json *
ext-mongodb ^1.8.1
jean85/pretty-package-versions ^1.2
php ^7.0 || ^8.0
symfony/polyfill-php80 ^1.19

requires (dev)
squizlabs/php_codesniffer ^3.5, <3.5.5
symfony/phpunit-bridge 5.x-dev

I’m not sure what the issue is at this point. It’s possible something is wrong with the autoloader, so I dumped it and had composer regenerate it. No dice. Do you guys have any thoughts on what the cause might be?

I’m running php 7.4 on Windows 10 via XAMPP.

where did you define SITE_ROOT ?

SITE_ROOT is a constant defined in config.php that points to site’s root directory, the file also lives at the root level.

<?php

// Path settings
define("SITE_ROOT", __DIR__);

Simplify your example.

  • Create a dir.
  • Composer install the mongodb php library in that dir
  • Create a file in that dir with a simple require for ./vendor/autoload.php
  • Access your collection with a mongodb uri

See if that works.

That appears to work. So that confirms the library is working. So something is probably wrong with the import. I confirmed that SITE_ROOT has the correct value, and the the path to autoload.php is correct.

var_dump
C:\xampp\htdocs\index.php:11:

object(MongoDB\Collection)[7]
private ‘collectionName’ => string ‘sources’ (length=7)
private ‘databaseName’ => string ‘waw’ (length=3)
private ‘manager’ =>
object(MongoDB\Driver\Manager)[8]
public ‘uri’ => string ‘mongodb://127.0.0.1/’ (length=20)
public ‘cluster’ =>
array (size=0)
empty
private ‘readConcern’ =>
object(MongoDB\Driver\ReadConcern)[13]
private ‘readPreference’ =>
object(MongoDB\Driver\ReadPreference)[14]
public ‘mode’ => string ‘primary’ (length=7)
private ‘typeMap’ =>
array (size=3)
‘array’ => string ‘MongoDB\Model\BSONArray’ (length=23)
‘document’ => string ‘MongoDB\Model\BSONDocument’ (length=26)
‘root’ => string ‘MongoDB\Model\BSONDocument’ (length=26)
private ‘writeConcern’ =>
object(MongoDB\Driver\WriteConcern)[15]

1 Like

I figured it out! So I’ve been using PHP storm to edit my code in C:/git/waw. PHP storm automatically pushes my code to C:/xampp/htdocs. Because of this, it never occurred to me that there might be a difference between those two directories, however it seems that PHP storm doesn’t push files added by composer inside the vendor file automatically. So library was in fact missing on the server, but not in my local repo.

TLDR: I’m a moron and should have checked the vendor file on the server, not just the repo.

1 Like

Glad you figured it out, best of luck in your project.

Thanks for the help!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.