Mongodb With PHP Step By Step Tutorial

In this tutorial we will learn mongodb with php. In this tutorial we will learn mongodb installation step by step and connect with php and create collection/database.
First we have to install mongo db with following steps:
1) Download Mongo db msi file from this link
2) Run downloaded mongodb msi file.
3) After installation mongodb open command prompt and run following command:

Above command will connect to http://localhost:27017 which is mongodb port.

4) Now open another command prompt and run following command :

Now connect to mongodb shell with test database like:

Now we will move to the PHP to connect with mongodb step by step process.

Firstly connect mongodb with php need to load mongodb driver. Now need to download mongodb drivers search in google or go to direct link which is : in this link go to the Drivers section and check php and click Releases or this link ( go to Available Releases section and click on DLL to download latest release, before download please make sure your php version 5.5 or 5.6 or 7.1 with 32 or 64 bit and also check your php thread safe or not using available file inside php folder in xampp like : php5ts.dll or php6ts.dll

According above instruction download compatible mongo driver and extract and copy php_mongodb.dll file and paste on ext folder which is on c:\xampp\php\ext
Now has to open c:\xampp\php\php.ini file and find {extension} and include “extension=php_mongodb.dll” and restart apache server.

follwed by above step your mongodb successfully installed and php mongodb driver also installed. Now using the composer we will download mongodb library.

If you not have composer for php then you have to download composer from go to this link and download Composer-Setup.exe.

After installation of composer open cmd(command prompt) and got to your project directory like:

After run above command result look like below data:

Now you will see in your project directory compose.json file and vendor directory.
For run mongo run two command one by one in separate command prompt first run mongod.exe with path of db and second mongo.exe to mongodb cell.

For testing mongodb with php create one php file with the name phpmongo.php and paste below code.

Run your php file on http://localhost/phpmongo/phpmongo.php you will see result like :

If you find above data on your page that means you are successfully connected with your mongodb database server.

Leave a Reply

Your email address will not be published. Required fields are marked *