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 https://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.

Leave a Reply

Your email address will not be published.

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

Most Popular Posts

How To Reset resourceID in MODx

Posted on 11 years ago

Bhumi

Getting Started with LESS CSS

Posted on 9 years ago

Bhumi