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:

C:\Program Files\MongoDB\Server\3.2\bin>mongod.exe --dbpath E:\MongoDB\data\db

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

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

C:\Program Files\MongoDB\Server\3.2\bin>mongo.exe

Now connect to mongodb shell with test database like:

2017-06-22T21:34:49.519+0530 I CONTROL  [main] Hotfix KB2731284 or later update
is not installed, will zero-out data files
MongoDB shell version: 3.2.8
connecting to: test

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:

C:\xampp\htdocs\phpmongo>composer require "mongodb/mongodb=^1.0.0"

After run above command result look like below data:

“./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing mongodb/mongodb (1.0.5): Downloading (100%)
Writing lock file
Generating autoload files”

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.

< ?php
require 'vendor/autoload.php';
//Connect With Mongo DB

$clientMongo = new MongoDB\Client("mongodb://localhost:27017"); 
// select a database

$companydb = $clientMongo->companydb;
$result = $companydb->createCollection('mycollection'); 


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

object(MongoDB\Model\BSONDocument)#9 (1) { ["storage":"ArrayObject":private]=> array(1) { ["ok"]=> float(1) } }

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