Docs Menu

MongoDB PHP Driver

Welcome to the documentation site for the official MongoDB PHP driver. You can add the driver to your application to work with MongoDB in PHP. The MongoDB PHP Driver consists of the following components:

  • Extension: Provides a low-level API and mainly serves to integrate the libmongoc and libbson libraries with PHP.

  • Library: Provides a high-level API for working with MongoDB databases consistent with other MongoDB language drivers.

While it is possible to use only the extension, we recommend using the extension and the library together. To learn more about the components of the PHP driver, see the Driver Architecture section of this page.

View the following guides to learn more about the driver and access tutorial content on setting up a runnable project:

This section describes how the components of the PHP driver work together. These components fit into the following general categories:

  • High-Level API, which includes the library and other integrations

  • Extension, which includes the extension that integrates the system libraries

  • System, which includes the C Driver, BSON library, and encryption library

The following diagram illustrates the architecture of the PHP driver components:

PHP driver component architecture

The PHP library provides an API that is consistent with the other MongoDB drivers. The library is continually updated to meet cross-driver specifications. You must add the library as a dependency to access MongoDB in most PHP applications.

The extension is distributed by using PECL, and connects PHP to the system libraries. The extension's public API provides the following functionality:

  • Connection management

  • BSON encoding and decoding

  • Object document serialization

  • Command execution

  • Cursor management

To learn more about the system libraries, see the C Driver documentation.

You can use the PHP driver to connect to deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB

  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB

Make sure you have a recent version of PHP installed on your system. See the PHP manual for download and installation instructions.

Install the PHP MongoDB Extension before you install the MongoDB PHP Library. You can install the extension by using PECL on the command line:

sudo pecl install mongodb

After installation completes, add the following line to your php.ini file:

extension=mongodb.so

Note

On some systems, there might be multiple .ini files for individual SAPIs, such as CLI, FPM. Make sure to enable the extension in all SAPIs that you use.

Then, install the PHP library by using Composer. Run the following command from your project directory:

composer require mongodb/mongodb

After installation completes, ensure that your application includes Composer's autoloader as shown in the following example:

<?php
require_once __DIR__ . '/vendor/autoload.php';

The PHP Library documentation contains reference materials and tutorials that demonstrate how to interact with your MongoDB data.

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 MongoDB\BSON\Int64 instance instead of a PHP integer type.

The following compatibility table specifies the recommended version or versions of the PHP driver for use with a specific version of MongoDB.

The first column lists the driver version.

Important

MongoDB ensures compatibility between the MongoDB Server and the drivers for three years after the server version's end of life (EOL) date. To learn more about the MongoDB release and EOL dates, see MongoDB Software Lifecycle Schedules.

PHP Driver Versions
MongoDB 8.0
MongoDB 7.0
MongoDB 6.0
MongoDB 5.0
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6

ext + lib 1.20 to 1.21

ext + lib 1.16 to 1.19

ext + lib 1.15

The following compatibility table specifies the recommended version or versions of the PHP driver for use with a specific version of PHP.

The first column lists the driver versions.

PHP Driver Versions
PHP 8.4
PHP 8.3
PHP 8.2
PHP 8.1
PHP 8.0
PHP 7.4
PHP 7.3
PHP 7.2

ext + lib 1.21

ext + lib 1.20

ext + lib 1.17 to 1.19

ext + lib 1.16

ext + lib 1.15

For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.