Back to Top

How to use NoSQL and MongoDB


In my last post, we checked basics about NoSQL database.If you have not read about NoSQL yet, read it In this article, I will show you how you can use MongoDB database.

What is MongoDB

MongoDB is document oriented database means everything in MongoDB is in a document and documents represented as JSON.

MongoDB is mainly implemented for several reasons:

  1. Its Uses JSON rather than XML
  2. Its Scalable
  3. High Performance
  4. Support Indexing
  5. Fast and easy to use

MongoDB is schema-less and there is not a notion of rigid table Structure.Data stored in MongoDB as the document so there is no need of Join Operation. At first it might seem odd but as you will see, its takes data quite nicely and easily.

How to install MongoDB in Windows:

Here are the steps to install MongoDB in Windows


Step 1:

First of all download dll file for MongoDB (as per version of PHP you are using),php_mongo.dll from here. After than add php_mongo.dll file in your php extension directory(Mostly its path like C:PHPext). Next is to add below line in your php.ini file.


Step 2:

After following first step, Create one folder or directory in c drive like below:

c:> mkdirdata
c:> mkdirdatadb

Step 3:

Now download MongoDB from Here. Unzip the downloaded folder and rename it to “Mongo”.

Step 4:

To connect with database, first of all, run the mongod.exe from command prompt

C:> cd Mongobin
C:Mongobin> mongod

and your MongoDB server will start running.After that start to run the mongo.exe file.

C:> cd Mongobin
C:Mongobin> mongo

And That’s all. you are connected with MongoDB.

How to connect MongoDB Database

As I have explained above that MongoDB stores data as documents which is like a simple associative array. So All the queries and operations in MongoDB are performed with the array.

The database connection code is very similar to code for connecting with MySQL or other types of databases:

To Connect with database server at localhost port 27017:

$connection = new Mongo();

To Connect with a remote host with optional custom port:

$connection = new Mongo( "" );
$connection = new Mongo( "" );

When the connection to MongoDB server is established, Now it is a time to select a database to work with. If no database exists, a new database is created.

Currently there are two ways to do this:

$db = $connection->selectDB('dbname');
$db = $connection->dbname;

Note : In MongoDB, tables are known as collections.

Then we need to select a collection of selected database like we would pick a table of selected relational databases.

$collection = $db->selectCollection('person');

or simply

$collection = $db->person;

How to insert New documents in MongoDB

The collection object should be used to perform basic operations to manipulate its information. For instance, if want to store information about a person you use code like this:

$person = array(
			'name' => 'Bhumi Shah',
			'email' => '[email protected]',
			'address' => array(
						'country' => 'MI',
						'zip' => '4821',
						'address1' => '5 Corner Street'
						'country' => 'CA',
						'zip' => '4830',
						'address1' => '5925, Wall street'
			'sessions' => 0,
$safe_insert = true;
$collection->insert($person, $safe_insert); // To add record in document.
$person_identifier = $person['_id'];

How to update existing documents in MongoDB

$record = array('email => '[email protected]');
$new_data = array(
		'$inc' => array('sessions' => 1),
		'$set' => array(
		'address.0.address2' => 'Some other address',
		'$unset' => array('address.1' => 1),
$options['multiple'] = false;// To update single record
$collection->update($record, $new_data, $options); //To update record

Above statement update document by adding new field address2 for user “[email protected]” and unset address.1 will unset the address1 field from the document. multiple = false is for to update the single record with email id “[email protected], if true update multiple records.

How to Delete documents in MongoDB

Deleting a document is very similar to retrieving or update documents.

$record = array('field' => 'address');
$collection->remove($record); // To delete record from the document

The main purpose to use MongoDB is speed and its simplicity because most databases are too much slow to fetch data and PHP needs to wait for databases most of the time and web development is impossible without database so if you want to work fast with the database, MongoDB is best option you have.

The collection object should be used to perform basic operations to manipulate its information. For instance, if want to store information about a person you use code like this:

Let’s see one example of Contact form

Contact form contain contact information of user like user contact number if user has multiple contact number like residential, mobile number, company number and even if you want to store like this more information then you need to take relational table for same and if we move to relational table, we need to apply Join query to retrieve information of user in our traditional database whereas in MongoDB everything is stored in document so there is no need of JOIN operation.

Everything stored in MongoDB is in one document, all addresses are associated in the same record without JOINs and if I would like to include a collection of contact information of a user, I can simply create a embedded docs in MongoDB.

I hope you have enjoyed this short post. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates. Do you have any question in MongoDB? Let me know via comment section.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to Install WAMP Server on Windows 8

Posted on 6 years ago


How to upload file using WordPress

Posted on 6 years ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago