Installing the PHP 7 MongoDB Driver Plugin
MongoDB is a document-oriented database that is highly scalable. It differs from relational databases in that it stores data in collections, rather than tables. Each collection is similar to a PHP array.
MongoDB also supports indexes on collections to improve query performance, as well as aggregation. It can be installed in two ways: through an extension and through the library.
1. Installation
MongoDB is a popular cloud & server-based database. It is used for document databases & is known for its flexibility & scalability. The PHP driver plugin enables PHP to interact with MongoDB. It is important to install the proper PHP version before installing the MongoDB driver. The simplest way to check the installed version is by entering php -v or pecl in a terminal window.
The MongoDB PHP library and extension provide a high-level API that is consistent with other language drivers. The library is continuously updated to meet cross-driver specifications. You can use the php-mongodb pecl repository to install the latest version of the library. This process is similar to installing a PEAR package on macOS or a UNIX-like system such as Linux.
2. Configuration
In the Atlas web interface, click the green button labelled “Get Started”. This will provide a checklist of steps to get your cluster up and running. It will also prompt you to load some sample data into your cluster.
The mongo extension is the low-level PHP layer that communicates with a MongoDB server. It is a PECL extension, which means that it is available for multiple versions of PHP. It currently supports 5.4, 5.6, 7.0, 7.1, and 7.3.
Before you install the MongoDB driver, make sure that your version of PHP is compatible with it. You can do this by using the YUM command to check your installed PHP version. This can be done on macOS or a UNIX-based system, such as Linux. This command will also list the PEAR repository that is configured for your system.
3. Development
The php mongodb driver provides a low-level API that allows PHP to connect to a MongoDB database. It mainly serves to integrate libmongoc and libbson with PHP. This driver should be used in conjunction with a higher-level library, such as the MongoDB PHP Library.
The library provides a more robust API than the extension, and it models client, database, and collection objects. It also provides methods for executing CRUD operations and managing indexes on collections. The new library is recommended over the old legacy driver extension for most purposes, as it will be more future-proof.
Before developing, it is advisable to load sample data into the database. This can be done from the Atlas web interface. The easiest way to do this is by using the “Get Started” button at the bottom-left of the page.
4. Testing
The mongodb extension provides a low-level API and essentially functions as a bare bone layer that allows PHP to interact with MongoDB. The MongoDB PHP Library, on the other hand, provides a high-level abstraction that models client, database and collection objects to provide a rich set of features.
The simplest way to test whether the PHP library and MongoDB are functioning correctly is to use the mongodb-tester tool. After the tool verifies a good connection, it will create a new phptest collection and insert some data into it.
The php-mongodb package is available as a PECL extension for PHP 5.4, 5.6 and 7.1, and it can be installed using the yum command on Linux and macOS. You must install MongoDB before installing the PECL extension.
5. Deployment
The PHP Driver for MongoDB is available via the PHP Extension Community Library (PECL). There are a number of ways to install it on macOS and UNIX-like systems such as Linux. However, it is recommended that the MongoDB extension and library be installed together. The extension provides a low-level API, while the library provides a higher level of abstraction that is consistent with other MongoDB language drivers.
Once the driver has been installed, it is ready to be used in your production application. To check whether it is functioning correctly, run a phpinfo() function on your machine and see if MongoClient information is displayed. Alternatively, log in to your NodeChef platform account and complete the deployment process from the dashboard. The platform will then host the environment and a MongoDB node.