Get Started with the PHP Library
On this page
- Overview
- Download and Install
- Install dependencies
- Install the MongoDB PHP extension
- Update your PHP configuration file
- Create a project directory
- Install the MongoDB PHP Library
- Create a MongoDB Deployment
- Create a free MongoDB deployment on Atlas
- Save your credentials
- Create a Connection String
- Find your MongoDB Atlas Connection String
- Copy your Connection String
- Update the Placeholders
- Run a Sample Query
- Edit your PHP application file
- Assign the connection string
- Run your PHP application
- Next Steps
Overview
The MongoDB PHP Library is a high-level abstraction for the MongoDB PHP extension, which you can use to connect to MongoDB and interact with data stored in your deployment. This guide shows you how to create an application that uses the MongoDB PHP Library to connect to a MongoDB cluster hosted on MongoDB Atlas and query data in your cluster.
Tip
MongoDB Atlas is a fully managed cloud database service that hosts your MongoDB deployments. You can create your own free (no credit card required) MongoDB Atlas deployment by following the steps in this guide.
Follow this guide to connect a sample PHP application to a MongoDB Atlas deployment. If you prefer to connect to MongoDB using a different driver or programming language, see our list of official drivers.
Download and Install
Create a project directory
From your root directory, run the following command in your shell to create
a directory called php-quickstart
for this project:
mkdir php-quickstart
Select the tab corresponding to your operating system and run the following commands
to create a quickstart.php
application file in the php-quickstart
directory:
cd php-quickstart touch quickstart.php
cd php-quickstart type nul > quickstart.php
Install the MongoDB PHP Library
To install the MongoDB PHP Library, run the following command in your php-quickstart
directory:
composer require mongodb/mongodb
After installing the library, include Composer's autoload.php
file by adding the
following code to the top of your quickstart.php
file:
require_once __DIR__ . '/vendor/autoload.php';
After you complete these steps, you have a new project directory, a new application file, and the library dependencies installed.
Create a MongoDB Deployment
You can create a free tier MongoDB deployment on MongoDB Atlas to store and manage your data. MongoDB Atlas hosts and manages your MongoDB database in the cloud.
Create a free MongoDB deployment on Atlas
Complete the Get Started with Atlas guide to set up a new Atlas account and load sample data into a new free tier MongoDB deployment.
After you complete these steps, you have a new free tier MongoDB deployment on Atlas, database user credentials, and sample data loaded into your database.
Create a Connection String
You can connect to your MongoDB deployment by providing a connection URI, also called a connection string, which instructs the driver on how to connect to a MongoDB deployment and how to behave while connected.
The connection string includes the hostname or IP address and port of your deployment, the authentication mechanism, user credentials when applicable, and connection options.
Find your MongoDB Atlas Connection String
To retrieve your connection string for the deployment that you created in the previous step, log in to your Atlas account and navigate to the Database section and click the Connect button for your new deployment.

Then, select your user from the Select database user selection menu. Select "PHP" from the Driver selection menu and the version that best matches the version you installed from the Version selection menu.
Select the String tab in the Add connection string into your application code step to view only the connection string.
After completing these steps, you have a connection string that corresponds to your Atlas cluster.
Run a Sample Query
After retrieving the connection string for your MongoDB Atlas deployment, you can connect to the deployment from your PHP application and query the Atlas sample datasets.
Edit your PHP application file
Copy and paste the following code into the quickstart.php
file, which queries
the movies
collection in the sample_mflix
database:
require __DIR__ . '/../vendor/autoload.php'; use MongoDB\Client; $uri = getenv('MONGODB_URI') ?: throw new RuntimeException( 'Set the MONGODB_URI environment variable to your Atlas URI' ); $client = new MongoDB\Client($uri); $collection = $client->sample_mflix->movies; $filter = ['title' => 'The Shawshank Redemption']; $result = $collection->findOne($filter); if ($result) { echo json_encode($result, JSON_PRETTY_PRINT); } else { echo 'Document not found'; }
Assign the connection string
Assign the MONGODB_URI
environment variable to the connection string that you copied
from the Create a Connection String step of this guide. You can assign this
variable by running a shell command or creating a .env
file in your application,
as show in the following tabs:
export MONGODB_URI=<connection string>
MONGODB_URI=<connection string>
Run your PHP application
In your project directory, run the following shell command to start the application:
php quickstart.php
The command line output contains details about the retrieved movie document:
{ "_id": { "$oid": "..." }, ... "rated": "R", "metacritic": 80, "title": "The Shawshank Redemption", ... }
If you encounter an error or see no output, ensure that you assigned the
proper connection string to the MONGODB_URI
environment variable and
that you loaded the sample data.
After you complete these steps, you have a PHP application that connects to your MongoDB deployment, runs a query on the sample data, and returns a matching document.
Next Steps
Congratulations on completing the quick start tutorial!
Note
If you run into issues in this tutorial, ask for help in the MongoDB Community Forums or submit feedback by using the Rate this page tab on the right or bottom right side of this page.
In this tutorial, you created a PHP application that connects to a MongoDB deployment hosted on MongoDB Atlas and retrieves a document that matches a query.
Learn more about the MongoDB PHP Library from the following resources:
Learn how to perform read operations in the Read Data section.
Learn how to perform write operations in the Write Data to MongoDB section.