PHP Connection to MongoDB
PHP Connection to MongoDB
This guide shows how to connect a PHP application hosted on the platform to MongoDB. The workflow covers MongoDB environment creation, database and user setup, activation of the PHP MongoDB driver, application deployment, and connection verification.
Create the Environment
Create a MongoDB environment
Create a new environment and add a MongoDB node of the required version from the NoSQL section. Add any other required nodes as well. The source example adds an Apache PHP server for the test application, although the PHP server can also be located in a separate environment.

Configure the required resources, region, environment name, and other parameters, then click Create.
MongoDB Configurations
Check the MongoDB access details
After environment creation, check the email sent by the platform. It contains MongoDB instance details and access credentials.

Open the MongoDB administration panel
Use the Access URL from the email or click Open in Browser for the MongoDB node. Enter the supplied administrator credentials and click Login.

Create a database
Open the Databases tab and create a separate database for the application. The source example uses mongodb-connect.

Create a database user
Open the Execute tab and run the following command:
db.createUser({
user: "user_name",
pwd: "password",
roles: [
{ role: "readWrite", db: "db_name" }
]
})

Select the corresponding database from the drop-down list below the command field and click Execute.
Enable the PHP MongoDB Driver
PHP application server builds include two MongoDB driver versions:
mongo.so— deprecated legacy drivermongodb.so— current driver used for new projects
Driver recommendation
The source recommends keeping mongo.so only for older applications that depend on its legacy API and using mongodb.so for new projects.
Return to the dashboard and click Config for the PHP application server.

Open etc/php.ini, locate the required MongoDB extension line, and remove the leading semicolon to enable it.

Save the configuration and restart the application-server node.

Application Deployment
Deploy the PHP application using Deployment Manager from a packaged archive or directly from a Git/SVN repository. The source test application uses the newer mongodb.so extension to verify the connection.
The example builds the connection with MongoDB\Driver\Manager, writes three documents, and queries the inserted data:
$manager = new MongoDB\Driver\Manager(
"mongodb://{$host}/{$userdb}",
array(
"username" => $username,
"password" => $password
)
);
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1]);
$bulk->insert(['x' => 2]);
$bulk->insert(['x' => 3]);
$manager->executeBulkWrite($database, $bulk);
$filter = ['x' => ['$gt' => 1]];
$options = [
'projection' => ['_id' => 0],
'sort' => ['x' => -1]
];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery($database, $query);
The source also provides a ready-made test package. A separate test application is available for the deprecated mongo.so driver.

Connection Check Up
Open the test application
Click Open in Browser for the PHP environment. The test form requests the MongoDB connection details.

https:// part.dbuser.mongodb-connect in the source example.Enter the connection values and click Test Me!.
If the details are correct, the application displays Good connection. Otherwise, it displays a connection failure message together with the error details.

Verify the test collection
A successful connection also creates a new phptest collection with several records in the selected database. Open the MongoDB administration panel and confirm that the collection and records are present.

Expected Result
The PHP application connects successfully to MongoDB using the enabled mongodb.so driver, authenticates with the configured database user, writes test records, performs a query, displays a successful connection message, and creates the phptest collection in the selected MongoDB database.
Important Notes
- The MongoDB and PHP application servers can be in the same or separate environments.
- The source example uses Apache PHP as the application server.
- The example database is
mongodb-connect. - The database user receives the
readWriterole. mongo.sois deprecated; the source recommendsmongodb.sofor new projects.- Enable the selected driver in
php.iniand restart the application server. - Use the MongoDB admin-panel host without the
https://prefix in the connection test. - A successful test creates the
phptestcollection.
