Navigation

MongoDB PHP Driver

Introduction

The currently maintained driver is the mongodb extension available from PECL.

You might consider using the driver with the complimentary PHP library, which implements a more full-featured API on top of the bare-bones driver.

Further information on this architecture may be found in the PHP.net documentation.

Installation

You can install the driver using PECL:

$ pecl install mongodb
$ echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

The preferred method of installing the optional but recommended PHP library is with Composer by running the following from your project root:

$ composer require mongodb/mongodb

Additional installation instructions may be found in the library documentation.

Connect to MongoDB Atlas

To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:

<?php

$client = new MongoDB\Client(
    'mongodb+srv://<username>:<password>@<cluster-address>/test?retryWrites=true&w=majority'
);

$db = $client->test;

Compatibility

MongoDB Compatibility

The following compatibility table specifies the recommended version(s) of the MongoDB PHP driver for use with a specific version of MongoDB.

The first column lists the driver version(s).

In the table below, mongodb and PHPLIB refer to the MongoDB PHP driver and userland library, respectively.

PHP Driver MongoDB 4.0 MongoDB 3.6 MongoDB 3.4 MongoDB 3.2 MongoDB 3.0 MongoDB 2.6
PHPLIB 1.4 + mongodb-1.5  
PHPLIB 1.3 + mongodb-1.4  
PHPLIB 1.2 + mongodb-1.3    
PHPLIB 1.1 + mongodb-1.2    
PHPLIB 1.0 + mongodb-1.1      
mongodb-1.0        

For additional driver versions, see PHP Driver MongoDB Compatibility Reference.

Language Compatibility

The following compatibility table specifies the recommended version(s) of the MongoDB PHP driver for use with a specific version of PHP.

The first column lists the driver version(s).

In the table below, mongodb refers to the MongoDB PHP driver.

PHP Driver PHP 5.5 PHP 5.6 PHP 7.0 PHP 7.1 PHP 7.2 PHP 7.3
mongodb-1.5
mongodb-1.4  
mongodb-1.3  

How to get help