Navigation

MongoDB PHP Driver

Introduction

The PHP driver consists of two components, the MongoDB extension and library.

The extension provides a low-level API and mainly serves to integrate libmongoc and libbson with PHP.

While it is possible to use the extension alone, users are strongly encouraged to use the extension and library together. The library provides a high-level API consistent with other MongoDB language drivers.

Installation

Installation of the Library requires the Extension being installed first.

You can install the extension using PECL:

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

The preferred method of installing the 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

Due to potential problems representing 64-bit integers on 32-bit platforms, users are advised to use 64-bit environments. When using a 32-bit platform, be aware that any 64-bit integer read from the database will be returned as a MongoDBBSONInt64 instance instead of a PHP integer type.

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, ext and lib refer to the MongoDB PHP Extension and Library, respectively.

PHP Driver MongoDB 4.2 MongoDB 4.0 MongoDB 3.6 MongoDB 3.4 MongoDB 3.2 MongoDB 3.0 MongoDB 2.6
ext 1.6 + lib 1.5  
ext 1.5 + lib 1.4    
ext 1.4 + lib 1.3    
ext 1.3 + lib 1.2      
ext 1.2 + lib 1.1      
ext 1.1 + lib 1.0        
ext 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, ext and lib refer to the MongoDB PHP Extension and Library, respectively.

PHP Driver PHP 7.4 PHP 7.3 PHP 7.2 PHP 7.1 PHP 7.0 PHP 5.6 PHP 5.5
ext 1.6 + lib 1.5  
ext 1.5 + lib 1.4  
ext 1.4 + lib 1.3    
ext 1.3 + lib 1.2    

How to get help