Learn how to connect to a MongoDB database instance with one of the world's most famous programming languages: PHP.
Managed Databases (also called Cloud Databases) allow you to focus on building and deploying cloud applications while OVHcloud takes care of the database infrastructure and maintenance in operational conditions.
You can find an example in our GitHub examples repository.
Requirements
- a Public Cloud project in your OVHcloud account
- access to the OVHcloud Control Panel
- a MongoDB database running on your OVHcloud Public Cloud Databases (this guide can help you to meet this requirement)
- configure your MongoDB instance to accept incoming connections
- A PHP environment with a stable version and public network connectivity (Internet).
Concept
A MongoDB instance can be managed in multiple ways. One of the easiest, yet powerful, is to use a Command Line Interface (CLI), as shown in our guide: Connect to MongoDB with CLI.
Another way is to interact directly using programming languages, such as PHP. PHP is used in almost 80% of the websites in the world, such as Facebook, Wikipedia or WordPress. MongoDB provides PHP drivers, allowing us to connect and manage a MongoDB instance from code.
To do so, we will need to set up our PHP environment with MongoDB drivers, then configure our Cloud Databases for MongoDB instances to accept incoming connections, and finally code in PHP to perform a few example actions.
Instructions
Setup your PHP environment
To interact with your MongoDB instance with PHP, your development environment needs to be configured with:
- A compatible version of PHP;
- MongoDB PHP extension;
- Strongly recommended: the MongoDB PHP Library.
Please follow the official MongoDB documentation for PHP drivers to get the latest information.
At the end of the documentation page, you will find a compatibility matrix between PHP versions, MongoDB extensions, and library versions. Follow this accordingly.
If you can edit your PHP environment on your own, install extensions and libraries as detailed in this documentation page linked above.
In both cases, when executing a phpinfo() in your PHP environment, you should see information about the MongoDB driver as shown below:
MongoDB support | Enabled |
---|---|
MongoDB extension version | 1.20.0 |
MongoDB extension stability | stable |
libbson bundled version | 1.28.0 |
libmongoc bundled version | 1.28.0 |
libmongoc SSL | enabled |
libmongoc SSL library | OpenSSL |
libmongoc crypto | enabled |
libmongoc crypto library | libcrypto |
libmongoc crypto system profile | disabled |
libmongoc SASL | disabled |
libmongoc SRV | enabled |
libmongoc compression | enabled |
libmongoc compression snappy | disabled |
libmongoc compression zlib | enabled |
libmongoc compression zstd | disabled |
libmongocrypt bundled version | 1.11.0 |
libmongocrypt crypto | enabled |
libmongocrypt crypto library | libcrypto |
crypt_shared library version | unknown |
We will now follow the official MongoDB documentation to perform our first connection with PHP.
Connect with PHP
In your PHP environment, let's try a connection. To be sure that we are indeed connected, we will dump all the MongoDB instance information.
As shown in the code, we use the MongoDB\Driver\Manager. Use the official PHP website for exhaustive documentation.
If you are connected, result of the var_dump($m) should be similar to this:
If the var_dump($m) is empty, you will have to analyze your PHP Environment and MongoDB compatibility matrix linked previously, and throwables:
- Connection timeout: usually due to incorrect IP authorization.
- Invalid namespace: the Service URI you entered may have a typo inside.
Once connected, you can perform multiple operations, for example, a bulk insert and a find() inside a collection:
In this example, we insert three objects inside a collection called mycol, and then we do a find() operation in this collection, looking for all the objects containing "bob".
The result here is for the var_dump($document) :
This code correctly found one object containing bob. It means that here we were able to connect to the MongoDB instance, insert data, and look for this data. Congratulations! Everything is working properly.
PHP frameworks and tools for MongoDB
Coding in PHP from scratch to interact with a MongoDB instance can be fun and very instructive. If you are interested, you can also take a look at the MongoDB PHP list for libraries, frameworks, and tools, which can assist your work with PHP.
Go further
For more information and tutorials, please see our other MongoDB and Cloud Databases support guides or explore the guides for other OVHcloud products and services.
If you need training or technical assistance to implement our solutions, contact your sales representative or click on this link to get a quote and ask our Professional Services experts for a custom analysis of your project.