In need of (possibly deprecated) MongoDB\Driver files

859 views
Skip to first unread message

Sean Gribbin

unread,
Sep 24, 2019, 1:54:58 AM9/24/19
to mongodb-user
Hi, I'm having quite a bit of trouble. You see, the PHP Driver doesn't have the MongoDB\Driver folder/files and I have absolutely no idea how to get them.
MongoDB is an extension in all my php.ini files, it's activated on my phpinfo(), I've done everything I can possibly think of and I just do not know how to get through this.

Please help. Here's a picture detailing my missing files and some of my file structure.

Jeremy Mikola

unread,
Sep 24, 2019, 10:29:33 AM9/24/19
to mongod...@googlegroups.com
On Tue, Sep 24, 2019 at 1:54 AM Sean Gribbin <hari...@gmail.com> wrote:
Hi, I'm having quite a bit of trouble. You see, the PHP Driver doesn't have the MongoDB\Driver folder/files and I have absolutely no idea how to get them.
MongoDB is an extension in all my php.ini files, it's activated on my phpinfo(), I've done everything I can possibly think of and I just do not know how to get through this.

Classes within the MongoDB\Driver namespace are implemented by the "mongodb" extension and reside within the repository's src/ directory. I see your project's lib directory contains files such as "MongoClient.php", which suggests your app might have been written for the legacy "mongo" extension, which did provide such a class. The current extension and userland library have abandoned using "mongo" as a prefix in favor of a "MongoDB" namespace (e.g. MongoDB\Client class in the library).

I'd suggest you take a look at the PHP driver portal and walk through the installation steps to confirm that you have the correct extension installed. That page no longer links to the unmaintained legacy extension, but its related links are below (so you can compare with the links for the current extension and library on the portal page):

Sean Gribbin

unread,
Sep 24, 2019, 12:14:38 PM9/24/19
to mongodb-user
Hi there Jeremy!

I really appreciate your response. The pecl installation package doesn't work. 
pecl install mongodb
returns
"No releases available for "pecl.php.net/mongodb"" even after upgrading the channels.
Here are the things that I've done:

  • extension=php_mongodb.dll in php.ini
  • ran composer require mongodb/mongodb in project directory
  • pecl install mongodb doesn't work (the error tells me the package doesn't exist)
  • looked through source code of both the extension and php adapter https://github.com/mongodb/mongo-php-driver

Jeremy Mikola

unread,
Sep 24, 2019, 12:59:14 PM9/24/19
to mongod...@googlegroups.com
On Tue, Sep 24, 2019 at 12:14 PM Sean Gribbin <hari...@gmail.com> wrote:
Hi there Jeremy!

I really appreciate your response. The pecl installation package doesn't work. 
pecl install mongodb
returns
"No releases available for "pecl.php.net/mongodb"" even after upgrading the channels.
Here are the things that I've done:

  • extension=php_mongodb.dll in php.ini
  • ran composer require mongodb/mongodb in project directory
  • pecl install mongodb doesn't work (the error tells me the package doesn't exist)
  • looked through source code of both the extension and php adapter https://github.com/mongodb/mongo-php-driver
"php_mongodb.dll" hints that you're using Windows. If that's the case, the "No releases available" error you're seeing may be the result of attempting to run "pecl install" from Windows. Windows users need to download a DLL, as PECL is not able to compile the extension from source as it would otherwise due on Linux and macOS.

Side note: this wasn't clear from your original post. In the future, I'd suggest leading with as much detail as possible about your environment (e.g. OS, PHP version, web server version and deployment).

If you are adding "extension=php_mongodb.dll" to your PHP configuration, where did you obtain the DLL file? The extension's Windows install docs directs users to download a platform-specific DLL from https://pecl.php.net/package/mongodb. For the most recent 1.6.0 release, you'll find several options available on https://pecl.php.net/package/mongodb/1.6.0/windows. You'll need to select the appropriate download for your PHP version, system architecture (x64 or 32-bit x86), and threading model. You may find more information about the platform variations on https://windows.php.net/.

Beyond that, PHP itself should log an error if the "extension=" INI directive fails to load the extension, be it for a missing file or attempting to use a DLL built for a different platform. If you are not seeing any error attempting to load the DLL and PHP doesn't report that the "mongodb" extension is loaded at startup, I'd suggest looking into other INI directives that might be suppressing your error reporting. Additionally, it'd be wise to start with the CLI environment (e.g. "php.exe -m") before attempting to diagnose the problem from a web SAPI, as you might be able to more easily capture error information from the console vs. log files. Note that each PHP environment likely has its own binary and php.ini file, even if they may point to a common folder for other INI includes, such as extension-specific INI files.

Sean Gribbin

unread,
Sep 24, 2019, 2:55:32 PM9/24/19
to mongodb-user
> I'd suggest leading with as much detail as possible about your environment (e.g. OS, PHP version, web server version and deployment)
Apologies for not including this stuff in the beginning. I am using Windows 10-64bit build 18362, WAMP w/PHP 7.3.5, server API of Apache2. 

I did obtain the DLL file from the Windows Install Docs in which I downloaded the 7.3 thread-safe 1.6 version of MongoDB. 
This is what my error log from WAMP says:
[24-Sep-2019 18:44:12 UTC] PHP Fatal error:  Uncaught Error: Class 'MongoClient' not found in C:\wamp64\www\ThreeLockBox\app3lb\apislim\lib\MongoDriver.php:118
Stack trace:
#0 C:\wamp64\www\ThreeLockBox\app3lb\apislim\index.php(43): MongoDriver->__construct('local')
#1 {main}
  thrown in C:\wamp64\www\ThreeLockBox\app3lb\apislim\lib\MongoDriver.php on line 118

MongoDriver.php line 118 is:
$mongo = new MongoClient("mongodb://$user:$pass@$host",$opt);
I know for a fact MongoClient.php exists in the right directory (C:\wamp64\www\ThreeLockBox\app3lb\apislim\lib\)

Capture.PNG



MongoDB is shown in my phpinfo. I'd include a screenshot but it won't paste. it shows the ext version is 1.6.0 and the ext stability is stable. 
I'd like to mention that the file I showed in my op, MongoClient.php, was taken from the src here (client.php and renamed).

I really appreciate the responses, please let me know if there's anything more I can give.

Sean Gribbin

unread,
Sep 24, 2019, 3:48:09 PM9/24/19
to mongodb-user
okay so adding
require_once("C:\wamp64\www\ThreeLockBox\app3lb\apislim\lib\Client.php");
forces it to recognize Client.php and allows the code to continue. I'm then faced with very similar errors, so I added require_once references to those files. 
However, I'm met with an InvalidArgumentException:

Capture.PNG

Fatal error: Uncaught MongoDB\Exception\InvalidArgumentException: Expected $command to have type "array or object" but found "string" in C:\wamp64\www\ThreeLockBox\app3lb\apislim\lib\Exception\InvalidArgumentException.php on line 39
( ! ) MongoDB\Exception\InvalidArgumentException: Expected $command to have type "array or object" but found "string" in C:\wamp64\www\ThreeLockBox\app3lb\apislim\lib\Exception\InvalidArgumentException.php on line 39

Line 39 of InvalidArgumentException.php is

        return new static(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, is_object($value) ? get_class($value) : gettype($value)));

which... is the file InvalidArgumentException itself?

This leads me to believe that the require_once statements aren't actually useful.

>Additionally, it'd be wise to start with the CLI environment (e.g. "php.exe -m") before attempting to diagnose the problem from a web SAPI, as you might be able to more easily capture error information from the console vs. log files.

I would be very willing to do this if I was more comfortable with the CLI; running php.exe -m didn't actually do anything for me and "grep" isn't a recognized command.

Jeremy Mikola

unread,
Sep 24, 2019, 6:07:22 PM9/24/19
to mongod...@googlegroups.com
On Tue, Sep 24, 2019 at 3:48 PM Sean Gribbin <hari...@gmail.com> wrote:

>Additionally, it'd be wise to start with the CLI environment (e.g. "php.exe -m") before attempting to diagnose the problem from a web SAPI, as you might be able to more easily capture error information from the console vs. log files.

I would be very willing to do this if I was more comfortable with the CLI; running php.exe -m didn't actually do anything for me and "grep" isn't a recognized command.

On Tuesday, September 24, 2019 at 11:55:32 AM UTC-7, Sean Gribbin wrote:
MongoDB is shown in my phpinfo. I'd include a screenshot but it won't paste. it shows the ext version is 1.6.0 and the ext stability is stable. 
I'd like to mention that the file I showed in my op, MongoClient.php, was taken from the src here (client.php and renamed).

If the "mongodb" extension is appearing in phpinfo(), that should be enough to confirm that the appropriate DLL is being loaded. There should be no need to double-check from a CLI environment with "php.exe -m".

I think there is a separate issue with the way you're integrating the MongoDB PHP library into your application. The library is intended to be installed as a Composer package (per its install instructions). If you're using it some other way (e.g. copying its source into your project), you'll be responsible for manually configuring autoloading for classes and functions within the library.

It would also be best to leave the library code as-is rather than make direct modifications, as that will make it much more difficult to diagnose problems (and provide support if you're not using a released version of the package). If your goal is to simply make some library classes accessible via a different name, I think you'd do better to rely on functions like class_alias().

Is there a technical reason that you're renaming the client class to MongoClient? As that happens to be the original client class in the legacy driver, it begs the question if your application was written against the legacy "mongo" driver API. The Upgrade Guide article in the library docs covers most API differences between the old and new driver and it's important to note that the new driver is not a drop-in replacement for the legacy extension. If you do need such a thing, the mongo-php-adapter library may be helpful (at least until you can allocate time to refactor your application code to the new driver's API).

okay so adding
require_once("C:\wamp64\www\ThreeLockBox\app3lb\apislim\lib\Client.php");
forces it to recognize Client.php and allows the code to continue. I'm then faced with very similar errors, so I added require_once references to those files. 
However, I'm met with an InvalidArgumentException:

Capture.PNG

Fatal error: Uncaught MongoDB\Exception\InvalidArgumentException: Expected $command to have type "array or object" but found "string" in C:\wamp64\www\ThreeLockBox\app3lb\apislim\lib\Exception\InvalidArgumentException.php on line 39
( ! ) MongoDB\Exception\InvalidArgumentException: Expected $command to have type "array or object" but found "string" in C:\wamp64\www\ThreeLockBox\app3lb\apislim\lib\Exception\InvalidArgumentException.php on line 39

Line 39 of InvalidArgumentException.php is

        return new static(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, is_object($value) ? get_class($value) : gettype($value)));

which... is the file InvalidArgumentException itself?

This leads me to believe that the require_once statements aren't actually useful.

Regarding this exception, it is thrown from the DatabaseCommand::__construct(), which is the last frame of your exception stack trace. I'm not sure why MongoDB\Client::selectDatabase() appears immediately before that frame, as the source of that function in the library does not construct a DatabaseCommand. That operation class is only constructed once in the library, from within MongoDB\Database::command().

In any event, the exception message should be clear and I don't think it's indicative of an issue with any require_once statements. The driver expected to receive an array or object as the command argument, but it is receiving a string instead. This type requirement is in place because commands in MongoDB are expressed as documents, as the PHP driver can convert both array and objects into BSON documents.

Sean Gribbin

unread,
Sep 24, 2019, 7:23:38 PM9/24/19
to mongodb-user
Because of the possible issue with the way I was integrating the PHP Library, I've essentially scrapped all edits and started from my provided baseline.
I do think that the application was originally put up against the deprecated mongo, I believe it was upgraded to MongoDB 3.0 at one point, though? I've been given a large codebase, so I apologize for not having all the information.

I've installed the PHP Library using Composer so I now have the composer.json and composer.lock files in C:\wamp64\www\ThreeLockBox\app3lb. Mongodb.dll is still added as an extension and shows up in phpinfo().

The reason I was renaming the Client class to MongoClient is because MongoClient simply doesn't exist in my files. I was holding out hope that simply changing the name would at least let me move on, but I guess that's not the way.
( ! ) Fatal error: Uncaught Error: Class 'MongoClient' not found in C:\wamp64\www\ThreeLockBox\app3lb\apislim\lib\MongoDriver.php on line 116
( ! ) Error: Class 'MongoClient' not found in C:\wamp64\www\ThreeLockBox\app3lb\apislim\lib\MongoDriver.php on line 116
Call Stack
#TimeMemoryFunctionLocation
10.0102412224{main}( )...\index.php:0
21.8116524712MongoDriver->__construct( ???, ??? )...\index.php:43

line 116: $mongo = new MongoClient("mongodb://$user:$pass@$host",$opt);


I'm really quite lost, I figured maybe starting from scratch and ensuring I installed everything correctly (which, at least this time around, I really believe I have) would help me but it's put me back at the same position I've been in since I've been given the codebase.

Sean Gribbin

unread,
Sep 24, 2019, 7:25:43 PM9/24/19
to mongodb-user
I don't think I can edit so I'd just like to briefly mention that the mongo-php-adapter requires MongoDB\Driver and its associated files which do not exist for me.

Tim Hawkins

unread,
Sep 24, 2019, 8:08:14 PM9/24/19
to mongodb-user
What version of PHP are you using? The legacy driver will only work for versions up to 5.6,  for 7.0 and up you have to use the new driver with the Mongodb php library to get compatability. 

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/dfbba9dd-3b01-463a-9de0-3e6acc78851f%40googlegroups.com.

Sean Gribbin

unread,
Sep 24, 2019, 8:09:00 PM9/24/19
to mongodb-user
Reading is important. I required the autoloader and that seems to have fixed (some of) my problems. Thanks again for being super patient, it's probably no surprise I'm a new dev,

That said, how could I go about fixing this? /var/ is a Unix directory, right?

( ! ) Warning: session_start(): open(/var/www/sessions\sess_dknjesnnn59o6rbf74g9lo9dve, O_RDWR) failed: No such file or directory (2) in C:\wamp64\www\ThreeLockBox\app3lb\apislim\index.php on line 67
Call Stack
#TimeMemoryFunctionLocation
10.0153414640
{main}( )...\index.php:0
20.5822617280session_start ( )...\index.php:67

( ! ) Warning: session_start(): Failed to read session data: files (path: /var/www/sessions) in C:\wamp64\www\ThreeLockBox\app3lb\apislim\index.php on line 67
Call Stack
#TimeMemoryFunctionLocation
10.0153414640
{main}( )...\index.php:0
20.5822617280session_start ( )...\index.php:67

Sean Gribbin

unread,
Sep 24, 2019, 8:26:45 PM9/24/19
to mongodb-user
Hi Tim,

I'm using PHP 7 and MongoDB 1.6.0. I realize the legacy driver doesn't work for me (considering my PHP version), would my life be a lot easier if I downgraded and went with PHP 5.6?
To unsubscribe from this group and stop receiving emails from it, send an email to mongod...@googlegroups.com.

Tim Hawkins

unread,
Sep 24, 2019, 8:36:17 PM9/24/19
to mongodb-user
It depends 

Php 5.6 is end of life, so it gets no more security fixes or patches. 

The new drivers that are compatibke with php 7.0+ have a different set of interfaces, and are essentialy a thin layer over the Mongodb C-Driver/Bson-Driver. 

There is a php library called the Mongodb PHP Library ( see: https://docs.mongodb.com/php-library/v1.2/ ), which provides a sort of adapter layer, that creates a very close emulation of the old drivers classes and methods ontop of the new mongodb driver.  We have ported a couple of old applications to the new driver using this layer. You install the new driver, and then autoload this library to give you a close analog of the old PHP extension. 





To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/35a5346d-6c1b-419f-be4c-1b0145d65908%40googlegroups.com.

Jeremy Mikola

unread,
Sep 25, 2019, 10:37:45 AM9/25/19
to mongod...@googlegroups.com
Note: I'm replying to several posts here, so I've quoted relevant bits as needed.
 
I do think that the application was originally put up against the deprecated mongo, I believe it was upgraded to MongoDB 3.0 at one point, though? I've been given a large codebase, so I apologize for not having all the information.

I don't believe the server version is relevant to our issue. IIRC, the legacy "mongo" extension's last version (1.6.x series) included support for MongoDB 3.0. All versions of the new "mongodb" extension and its corresponding PHP library also support MongoDB 3.0. The full compatibility table for the new driver is found on: https://docs.mongodb.com/ecosystem/drivers/php/#mongodb-compatibility


I don't think I can edit so I'd just like to briefly mention that the mongo-php-adapter requires MongoDB\Driver and its associated files which do not exist for me.

Earlier, you said that "MongoDB is shown in my phpinfo [...] it shows the ext version is 1.6.0 and the ext stability is stable." You also stated that "php_mongodb.dll" was being loaded, which corresponds to the newer "mongodb" extension. If that is correct, then you should absolutely have the MongoDB\Driver classes available. It's possible that you may have multiple PHP environments on your system, and one is successfully loading the extension (and its classes) but another is not. In this case, I'd suggest double-checking the environment where the MongoDB\Driver classes appear to be missing. Aside from phpinfo(), you can use functions such as phpversion()get_loaded_extensions(), or class_exists() to check that the extension and its classes are loaded. For example:

phpversion('mongodb'); // should return "1.6.0"
get_loaded_extensions(); // should return an array that includes "mongodb"
class_exists('MongoDB\Driver\Manager'); // should return true

I required the autoloader and that seems to have fixed (some of) my problems.

Glad to hear the autoloading was fixed. It may help to review the Composer documentation just in case you run into some issues down the line, though. It will walk you through the basis of its dependency management and how to properly include its autoloader: https://getcomposer.org/doc/00-intro.md


That said, how could I go about fixing this? /var/ is a Unix directory, right?

This looks like a more general PHP issue and would be better asked on forum such as Stack Overflow. The MongoDB driver doesn't interact with PHP sessions at all. At a quick glance, it looks like this might be related to the session.save_path INI setting.

There is a php library called the Mongodb PHP Library (see: https://docs.mongodb.com/php-library/v1.2/), which provides a sort of adapter layer, that creates a very close emulation of the old drivers classes and methods ontop of the new mongodb driver.  We have ported a couple of old applications to the new driver using this layer. You install the new driver, and then autoload this library to give you a close analog of the old PHP extension.

Small correction to Tim's comment here. The library he linked does provide a similar API to the legacy "mongo" extension, but I wouldn't refer to it as an adapter. Instead, the "mongodb" extension and "mongodb/mongodb" Composer library collectively fulfill the role of a MongoDB PHP driver, as the legacy "mongo" extension did on its own. The design of the current driver is explained in more detail in the Architecture Overview documentation in the "mongodb" extension's PHP.net documentation.

There is an actual adapter library (https://github.com/alcaeus/mongo-php-adapter/), which I linked in a previous comment. It depends on the new driver (i.e. "mongodb" extension and "mongodb/mongodb" library) and provides an identical API to the legacy "mongo" extension. That adapter was specifically developed with the goal of allowing legacy applications to more easily upgrade to PHP 7, since the "mongo" extension compatibility stopped with PHP 5.6. It is mainly used as a drop-in replacement for the "mongo" extension. The README file for that project should explain in more detail.

Lastly, I'll also point out that the current version of the library is 1.5 (Tim linked to the 1.2 docs). The documentation for the current version of the library can always be found at: https://docs.mongodb.com/php-library/current/

Reply all
Reply to author
Forward
0 new messages