MongoDB PHP Tutorial for Beginners

My previous article was about “Dependency Injection in PHP”. Today I would like to write about using MongoDB with PHP. This simple tutorial will help you how to start with popular NoSql database MongoDB and php. MongoDB it stores data as structured json format, which improves application performance and allowing you to write scalable application.

MongoDB and PHP tutorial

Installing MongoDB

Before diving into creating application using PHP and MongoDB let us see install MongoDb in your system. MongoDB runs on most of the platforms. I have provided few step below “how to install MongoDB in Ubuntu (Linux) system”.

Open your terminal and execute below command-

sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10

echo 'deb dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

Now update your local package database by executing below command and install latest stable version of MongoDB.

sudo apt-get update
sudo apt-get install mongodb-org

Here are few useful url for MongoDB installation. For more detail info of linux Installation

Windows Installation

You can download mongo driver from below link

Unzip the archive and put php_mongo.dll inside your php extension directory and add below line to your php.ini file.


Restart your apache server. That’s it! So we are done with the installation of MongoDB. Let us follow following steps to connect with mongo and do simple crud operations.

Connecting MongoDB with PHP

Connecting MongoDb is very easy and very similar with any other database abstraction class (PDO, Mysqli). By default MongoDb connect with the port 28017. You may access server info by simply entering.


Without login credentials.

$mongo = new MongoClient(); or 
$mongo = new Mongo();

With Credentials.

$host = ‘localhost’;
$port = ‘28017’;
$database = ‘test’;
$connecting =  sprintf('mongodb://%s:%d/%s', $host, $port, $database);
$mongo = new Mongo($connecting, array('username'=> $username, 'password'=> $password));

MongoDB default database is “test”. You can create your own database as below.

Selecting Database

$db = $mongo->your_database_name;

Creating Collections or Creating table

$collection = $db->products;

   $document = array( 
      "name" => "", 
      "description" => "The Technical Blog", 
      "url" => "",
      "by", "Sanjoy Dey"


Finding document

Find all document from your collections and iterate data to display title.

$collection = $db->products;
$data = $collection->find();

   foreach ($data as $document) {
      echo $document["name"] . "
"; }

Updating Document

Update document with simple update or save method.

$productArray =  array("name"=> "MongoDB");
$product = $collection->findOne($productArray);
$product [‘name’] = ‘MongoDB and PHP Tutorial’;
$product[‘by’] = ‘Sanjoy’;
$product[‘description’] = “Awesome! simple and best tutorial”;
try  {
} catch (MongoException $e) {
     echo $e->getMessage();

Deleting Document from Collection

Deleting document is as simple as other methods explained above. You can simple delete record from collections with remove method using the key.

$productArray = array( 'name' => 'MongoDB'); // or you can delete by id
 $collection->remove( $productArray, array( 'safe' => true ) );


So finally in this short article we have covered basic CRUD operation using PHP and MongoDB. You can explore much more to build large application. Hope this article helpful to you, if so please don’t forget to leave your comments, share and give a small like.

Keep visiting for latest post.

No comments:

Post a Comment

Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 All rights reserved. Powered By- Sanjoy Dey Productions