Monday 27 July 2015

Getting Started with MongoDB

In continuation to my previous blog post "MongoDB Basics" which gave an idea about what NoSQL DBMS is and what are the types and why we should use them, this blog also provides some further knowledge about how we can get start using MongoDB with GUI tool and take advantage as back-end service for our application development.

MongoDB

MongoDB is a NoSQL (i.e. non-relational) database. It is an open source DBMS which stores data in form of documents with JSON format. NoSQL databases provides high performance, scalability in comparison to any RDBMS (Relational database management system) when we have to deal with unstructured data in our application.

MongoDB is one of the popular NoSQL DBMS in comparison to others. It stores data inside documents (Objects) in form of JOSN format which makes faster performance and easily deal with datatypes of any programming language. MongoDB is beneficial as it provides some features same as any RDBMS like indexing, replication, sharding etc. In such way we can leverage the dual benefits as we are able to use features which we used to have with RDBMS with the other benefits of non-relational databases as well.

The most common use of NoSQL DBMSs’ are:
  •  To store real time data like: sensor based applications’ data.
  •  To store large amount of unstructured data in form of documents.
  • To maintain/archive old blog post data.
  • To maintain large amount of “Product catalog/Inventory” data, this is not relational in nature. Hence it is best to use NoSQL DB for this purpose.
  • Real time based applications.
  • To store comments from users for an e-commerce site.
  • Online gaming.

Configure MongoDB

This is very easy process to install, configure and use MongoDB.

Steps
  1. First we have to download MongoDB from official site:

http://mongodb.org/ and download Mongo.  While downloading it, make sure we choose correct version for windows (i.e. download should fit our system configuration, as for Windows 8 on a 64 bit processor we want “64 bit 2008 R2+”). This will provide msi file which we need to install
2.   Make sure entry inside environment variables: (inside PATH variable)

          “C:\Program Files\MongoDB\Server\3.0\bin\”.
      3.  After installation, check it will default store files inside our program files directory:
C:\Program Files\MongoDB\Server\3.0
            4.  Now we need to create a folder named MongoDB at any location (prefer to create at root level)
For example: D:\MongoDB
                       5.  In the folder created above we need to create a folder “data” and inside data folder create another folder “db”. 
            6.   Inside MongoDB folder create a text file, name it to “mongod.cfg” and paste following code there:

dbpath = D:\MongoDB\data\db
logpath = D:\MongoDB\mongod.log
logappend = true

Note: dbpath & logpath can be changed as per path in our system

            7.   To create service, run the command prompt as “Run as administrator”. Go to the path to bin folder of MongoDB in our system (i.e. C:\Program Files\MongoDB)
 Run this command:

sc.exe create MongoDB binPath= "C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe --service --config=\"D:\MongoDB\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"

After this MongoDB is up and running on our system as a service.




GUI tool to use MongoDB(Robomongo)

To easily use mongoDB we have GUI tool available.
First download it from official website: (Windows Installer)

1.       We need to install this tool into our system.
2.       Start robomongo tool and try to configure database. Inside MongoDB connection wizard click “create” link to establish connection.




3.       Here we can create connection to server by providing user authentication details inside “Authentication” tab and can provide default database name inside “Advanced” tab.
4.     After connecting to database server, we need to create our database. Right click on “New Connection” and select “Create Database”. Provide name to DB for example: “PeopleDB


 5.   This step create empty database and we have to then create collection, by right clicking on collections and select “Create Collection”. Provide name for example: “People”.
Note: For RDBMS folks it can be think of as a table.
6.    To get any data from collection we need to double click on collection name.
      7.     Now we need to insert some data to our People collection. By double clicking on it we can find shell script open at right side of window.
            8.     We need to write insert command there and press run. This will insert 1 record in our DB.


        9.     Double click on collection and see newly added data.


      10.    Here notice one additional field that has been added is _id and every document in MongoDB contains this id by default. We can have our own as well.
      11.   Now I am going to add one more record. As MongoDB is schema less so we can provide more information also.


      12.   Here notice power of MongoDB, as if we have to add some information to our table then we have to either add new column for that or create some other relational table but here we can easily add such data to document(as we don’t need to respect schema).
      13.   One more benefit of this tool is we can easily edit/ add data by right clicking on any document (record) and select “Edit document”.


      14.   Using mongoDB, we can have nested documents (i.e. document inside another document). This solves our problem as in SQL we had to create another table for this purpose.


      15.   This was the insertion of data. Now we have to query this inserted data. For example: If I have to find data with name: Tanya, I have to provide this condition inside find function.



     16.  Ordering data: We can also get data in specific order by providing sort function:

To sort in ascending order give 1:
db.People.find().sort({name:1})

To sort in descending order give -1:
db.People.find().sort({name: -1})

      17.  Using “equal/ greater than/ less than” operators:

To get record with age=25:
db.People.find({age:25})

To get record with age>18:
db.People.find({age:{$gt:18} })

To get record with age<18:
db.People.find({age:{$lt:18} })

Summary

So in this article we understood what MongoDB is, why we should use it and how we can start using it by GUI tool and how can we store data into it and query that data.
Please stay tuned for more information about how we can use MongoDB from C# application using C# driver.



No comments:

Post a Comment