Legacy MongoDB PHP driver
Latest commit 7dcbf42 Sep 5, 2017
Failed to load latest commit information.
.travis.scripts Remove the change to the compile script as we want streams enabled on… Jun 18, 2013
api PHP-1452: Rename extended bson_to_zval() function Jun 3, 2015
batch Merge branch 'v1.5' Nov 7, 2014
contrib PHP-1529: Ensure php-ssl.h has HAVE_MONGO_OPENSSL Sep 5, 2017
exceptions Remove pre-PHP 5.3 related macros. Sep 10, 2014
gridfs Use consistent types for chunk_size and pos in storeBytes() and store… Mar 16, 2016
mcon PHP-1468: Fix segfault on isMaster failure Apr 14, 2016
tests Merge branch 'v1.6' May 16, 2016
types Use const char* in bson.c where possible Jun 3, 2015
util Remove pre-PHP 5.3 related macros. Sep 10, 2014
.gitignore Sort out the includes a bit better and add support for a config.w32.h… Apr 25, 2013
.travis.yml PHP-1219: No more 5.2 in travis or package. Sep 10, 2014
CONTRIBUTING.md Fix make build target in contributing docs May 29, 2015
LICENSE.md Fix Markdown formatting in license file Dec 19, 2014
Makefile.frag coverage/ is a folder Sep 19, 2012
Makefile.servers Enable PHP5.5 and PHP5.4 testing in travis Apr 2, 2013
README.md Make sure that with the code in this snippet, the log always gets saved. Dec 17, 2014
bson.c PHP-1501: Rephrase exception message for unsupported/corrupt BSON type Mar 16, 2016
bson.h PHP-1452: Rename extended bson_to_zval() function Jun 3, 2015
cleantests.sh Update cleantests.sh for new test directories Apr 10, 2013
collection.c PHP-1511: append_gle should ignore unset fsync/journal from link Apr 25, 2016
collection.h PHP-953: Do not check custom or generated index name lengths Dec 8, 2014
command_cursor.c Remove redundant cast Mar 16, 2016
command_cursor.h PHP-1365: Validate cursor command option, but don't enforce batchSize Jan 7, 2015
config.m4 PHP-1528: Use SSL if linked instead of checking HAVE_OPENSSL_EXT Aug 30, 2017
config.w32 PHP-1528: Define HAVE_MONGO_OPENSSL when linking SSL on Windows Aug 31, 2017
cursor.c PHP-1460: Advancing cursor beyond its limit should kill it Jun 22, 2015
cursor.h ZTS fixes. Oct 8, 2014
cursor_shared.c If getmore fails, trust it also marks the cursor failed Jun 22, 2015
cursor_shared.h Use hexadecimal constants as that makes it clearer it's a bitfield Feb 3, 2015
db.c Fixed PHP-1477: Deprecate eval helpers Sep 1, 2015
db.h PHP-1285: Add MongoDB::getCollectionInfo as per SPEC-54. Nov 24, 2014
io_stream.c PHP-1529: Move HAVE_MONGO_OPENSSL usage below explicit config.h include Sep 5, 2017
io_stream.h Rename variable to be consistent with its friends Nov 18, 2014
log_stream.c PHP-1442: Don't use server options for stream IO notification May 5, 2015
log_stream.h PHP-1442: Don't use server options for stream IO notification May 5, 2015
mongo.c Remove pre-PHP 5.3 related macros. Sep 10, 2014
mongo.h Using MongoDB, Inc. And 2014. Mar 12, 2014
mongoclient.c Merge branch 'v1.5' Nov 28, 2014
mongoclient.h Use explicit pointer checks and namespace functions by module Jul 25, 2014
package.xml BUMP 1.6.0 Jan 29, 2015
php_mongo.c PHP-1528: Use SSL if linked instead of checking HAVE_OPENSSL_EXT Aug 30, 2017
php_mongo.h Merge branch 'v1.6' Mar 17, 2016
rebuild.sh Bump make parallelism and suppress stdout in rebuild script Mar 16, 2016


Build Status


To build and install the driver:

sudo make install

Then, add the following to your php.ini file:


Enabling enterprise features

To connect to MongoDB Enterprise using SASL (GSSAPI) or LDAP (PLAIN) you need to build the driver against cyrus-sasl2 (external library). This is done by passing --with-mongo-sasl to ./configure, optionally passing in the directory to where cyrus-sasl2 was installed:

./configure --with-mongo-sasl=/usr/local
sudo make install

Installing on Windows

Windows builds are available through http://pecl.php.net/package/mongo.

Builds for older driver versions may be found through https://s3.amazonaws.com/drivers.mongodb.org/php/index.html.

Each driver release includes various builds to support specific versions of PHP and Windows. Select the correct DLL file for your environment, and add the following to your php.ini file (VERSION will vary by environment):


If the DLL is not located within the directory specified by the extension_dir INI setting, you may need to specify its full path.


See the PHP manual.

How To Ask For Help

When asking for support, or while providing feedback in the form of bugs or feature requestes, please include the following relevant information:

  • Detailed steps on how to reproduce the problem, including a script that reproduces your problem, if possible.
  • The exact PHP version used. You can find this by running php -v on the command line, or by checking the output of phpinfo(); in a script requested through a web server.
  • The exact version of the MongoDB driver for PHP. You can find this by running php --ri mongo | grep Version on the command line, or by running a script containing <?php echo phpversion("mongo"); ?>.
  • The operating system and version (e.g. Windows 7, OSX 10.8, ...).
  • How you installed the driver, either via your distribution's package management, with "brew", with an *AMP installation package, or through a manual source compile and install.
  • With connection and replica set selection issues, please also provide a full debug log. See further down on how to make one.

Support / Feedback

For issues with, questions about, or feedback for the PHP driver, please look into our support channels. Please do not email any of the PHP driver developers directly with issues or questions—you're more likely to get an answer on the mongodb-user list on Google Groups.

Bugs / Feature Requests

Think you have found a bug? Want to see a new feature in the driver? Please open a case in our issue management tool, JIRA:

Bug reports in JIRA for all driver projects, as well as for the MongoDB server project, are public. Please do not add private information to bug reports.

Security Vulnerabilities

If you’ve identified a security vulnerability in a driver or any other MongoDB project, please report it according to the instructions here.


The tests are not available as part of the PECL package, but they are available on Github.

See CONTRIBUTING.md for how to run and create new tests.

Full debug log

You can make a full debug log by using the following code:

 * This script will catch all internal driver logging for MongoDB and log them
 * to a file /tmp/MONGO-PHP-LOG.<unix-timestamp>. This log will give the
 * driver developers more or less all the information they'll need to debug
 * this issue
function module2string($module)
    switch ($module) {
        case MongoLog::RS: return "REPLSET";
        case MongoLog::CON: return "CON";
        case MongoLog::IO: return "IO";
        case MongoLog::SERVER: return "SERVER";
        case MongoLog::PARSE: return "PARSE";
        default: return $module;
function level2string($level)
    switch ($level) {
        case MongoLog::WARNING: return "WARN";
        case MongoLog::INFO: return "INFO";
        case MongoLog::FINE: return "FINE";
        default: return $level;
function logMongo($module, $level, $message, $save = false) {
    static $log = "";
    $log .= sprintf("%s | %s (%s): %s\n", date('Y-m-d H:i:s',time()), module2string($module), level2string($level), $message);
    if ($save) {
        file_put_contents("/tmp/MONGO-PHP-LOG." . time(), $log);
        $log = "";
function saveMongoLogException($ex) {
    if ($ex instanceof MongoException) {
        $msg = sprintf("Uncaught exception: %s, %s\n",  get_class($ex), $ex->getMessage());
        logMongo(get_class($ex), "ERROR", $ex->getMessage(), true);
    throw $ex;

function saveMongoLog() {
    logMongo("EXIT", "EXIT", "EXIT", true);

/* If an global exception handler is used, or a default exception handler is
 * already registered, please comment out this line and put into your catch
 * block: saveMongoLogException($exception); */


Jon Moss

  • Came up with the idea and implemented MongoCursor implementing Iterator

Pierre-Alain Joye

  • Helped build the Windows extension and has provided the VC6 builds

Cesar Rodas

  • Created the MongoCursor::info method
  • Implemented GridFS read streaming

William Volkman

  • Made connection code check & handle error status

Derick Rethans

  • Implemented MongoInt32, MongoInt64 and related php.ini options.

Taneli Leppä

  • Provided a patch for PHP-706 to swap out select() for poll().