Back to Top

How to use NoSQL and MongoDB

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 http://www.thecreativedev.com/how-to-use-nosql-and-mongodb/here. 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

INSTALLATION:

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:

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

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

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:

To Connect with a remote host with optional custom port:

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:

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.

or simply

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:

How to update existing documents in MongoDB

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

How to Delete documents in MongoDB

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

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.

Share:
Share on Facebook0

Share on LinkedIn0Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit0

Leave a Reply

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

Most Popular Posts

How to set DirectoryIndex using .htaccess?

Posted on 2 years ago

Bhumi

Learn Responsive Web Design Online

Posted on 4 years ago

Bhumi

MySQL coalesce() function

Updated 17 years ago

Bhumi

How to use JavaScript Promise API?

Updated 17 years ago

Bhumi

What are Lambdas and Closures in PHP?

Updated 17 years ago

Bhumi

An Introduction to Gulp JS

Updated 2 months ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 3 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 4 years ago

Bhumi