Overview
This page contains code examples that show how to connect your PHP application to MongoDB with various settings.
Tip
To learn more about the connection options on this page, see the link provided in each section.
To use a connection example from this page, copy the code example into the
sample application or your own application.
Make sure to replace all placeholders in the code examples, such as
<hostname>
, with the relevant values for your MongoDB deployment.
Sample Application
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have the MongoDB PHP Library installed in your project. To learn more about installing the MongoDB PHP Library, see the Download and Install guide.
Copy the following code and paste it into a new
.php
file.Copy a code example from this page and paste it on the specified lines in the file.
1 2 3 require __DIR__ . '/../vendor/autoload.php'; 4 5 // Start example code here 6 7 // End example code here 8 9 try { 10 $client->test->command(['ping' => 1]); 11 echo 'Successfully pinged the MongoDB server.', PHP_EOL; 12 } catch (MongoDB\Driver\Exception\RuntimeException $e) { 13 printf("Failed to ping the MongoDB server: %s\n", $e->getMessage()); 14 }
Important
Percent-Encoding
You must percent-encode a username and password before
you include them in a MongoDB URI. You can use the rawurlencode()
method to encode
these values according to the URI syntax specified in RFC 3986.
Don't percent-encode the username or password when passing them in an options array
parameter to the MongoDB\Client
constructor.
Connection
Atlas
The following code shows how to connect to a MongoDB Atlas deployment:
$uri = '<Atlas connection string>'; $client = new MongoDB\Client($uri);
To learn more about connecting to an Atlas deployment, see Atlas in the Connection Targets guide.
Local Deployment
The following code shows how to connect to a local MongoDB deployment:
$uri = 'mongodb://localhost:27017/'; $client = new MongoDB\Client($uri);
Note
If you don't specify the $uri
parameter, the connection URI defaults to
'mongodb://127.0.0.1:27017'
.
To learn more about connecting to a local deployment, see Local Deployments in the Connection Targets guide.
Replica Set
The following code shows how to connect to a replica set deployment:
$client = new MongoDB\Client( 'mongodb://<replica set member>:<port>/', ['replicaSet' => '<replica set name>'], );
$uri = 'mongodb://<replica set member>:<port>/?replicaSet=<replica set name>'; $client = new MongoDB\Client($uri);
Tip
To maintain your connection to a replica set deployment when one host is down, you can provide multiple replica set members in the connection URI.
To learn more about connecting to a replica set, see Replica Sets in the Connection Targets guide.
Stable API
The following code shows how to enable the Stable API for the connection to your MongoDB instance:
$driverOptions = ['serverApi' => new MongoDB\Driver\ServerApi('1')]; $client = new MongoDB\Client( 'mongodb://<hostname>:<port>/', [], $driverOptions, );
To learn more about the Stable API, see the Stable API guide.
Network Compression
The following code shows how to specify the Snappy, Zlib, and Zstandard compression algorithms for a connection:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>', ['compressors' => 'snappy,zstd,zlib'], );
$uri = 'mongodb://<hostname>:<port>/?compressors=snappy,zstd,zlib'; $client = new MongoDB\Client($uri);
To learn more about network compression, see the Compress Network Traffic guide.