Saturday, 17 January 2015

Mongo for beginners

This artical is intended for Mongo DB beginners

Following items we are going to cover.

1. Introduction to NOSQL
2. Basic Mongo introduction
    2.1 Install of Mongo DB on Windows
    2.2 Install of Mongo DB on Linux
3. Basic lab using MongoDB
4. Java Integration

1. Introduction to NOSQL

     With the advent of the internet and the social networking and e-commerce sites, the data that started to accumulate was unstructured in format and the volumes were high. Moreover, the useful information for the companies was a part of the loads. For example: If a company wants to know the number of people that have twitted good about the company, then imagine the amounts of data that has to be filtered to get the result.

     The unstructured nature of data and the huge volumes started to bring a pinch to the traditional warehouses. To rescue these warehouses, HADOOP came into picture. HADOOP is a framework that allows distributed storage space that can handle bulks of data in whatever format it comes. Now this made it easy to handle and store large volumes of unstructured data.

     Soon it was realized that with the use HADOOP we had resolved the storage of unstructured data, but we were still facing the problem of querying that data. With the use of JOINS provide by RDMS,updating and fetching records became more and more difficult, resulted in slow transactions and more time consuming and costly. The need was felt of something more efficient, more reliable, and less tedious. This led to the advent of NoSQL databases.

Let see the basic difference and the features of NOSQL Databases

When we compare the NoSql database with any tradition RDBMS, the following differences comes to mind.



2. Basic Mongo introduction

In the basic Mongo introcution we will cover

  • What is Mongo DB
  • Install of Mongo DB on Windows
  • Start Mongod server and Mongo instance     

What is Mongo DB 


MongoDB is a document database that provides high performance, high availability, and easy scalability. In Document database the most popular ways of storing data is a document data model, where each record and its associated data is thought of as a “document”. In a document database, such as MongoDB, everything related to a database object is encapsulated together.

2.1 Install of Mongo DB on Windows
       
Follow below step to install Mongo DB

1. Click here to download Mongo for Windows. Please choose the required option for your windows. I am explaining the below steps with Zipped download.
2. Now extract the downloaded zip file. And rename the file to DeveloperMongoDB. See below




3. Now create one folder called MongoData under D:\DeveloperMongoDB. This directory is called data directoryMongoDB requires a data directory to store all data

4. Create one more folder called logs under D:\DeveloperMongoDB. Create one file called logs.txt, under newly created folder logs  D:\DeveloperMongoDB\logs\logs.txt

5. Create one file mongod.cfg under D:\DeveloperMongoDB with following content

logpath="D:\DeveloperMongoDB\logs\mongo.log"
dbpath="D:\DeveloperMongoDB\MongoData"

This completes your installation process on windows. Now lets see how to start the Mongo server

Start Mongo DB on Windows

1. Start command prompt and go to D:\DeveloperMongoDB\bin and then start Mongod server using mongod.exe --dbpath="D:\DeveloperMongoDB\mongo".

This starts the main MongoDB database process. The waiting for connections message in the console output indicates that the mongod.exe process is running successfully as show below


2. Now lets connect to mongo.exe shell. Open command prompt , go to D:\DeveloperMongoDB\bin and run "mongo.ex". Please refer image below



2.2 Install of Mongo DB on Linux

Follow the below steps to install on Linux

1. Add the MongoDB repository

[root@localhost mongo]# vim /etc/yum.repos.d/mongodb.repo

2. After executing the above command a file will get opened Press “ i “ to enter the Insert mode.

Type following for 64 bit machine and save the file

[mongodb] name=MongoDB Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/ 
gpgcheck=0
enabled=1

Type following for 32 bit machine and save the file

[mongodb] name=MongoDB Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686/
gpgcheck=0
enabled=1

After typing the above stuff press “ Esc “ button and type “ :wq “ and press Enter.

3. Install the MongoDB

[root@localhost mongo]# yum install mongo-10gen mongo-10gen-server

Verify the successfully installation message.

4. Start Mongod Sever

[root@localhost mongo]# service mongod start

or 

If you want to point to some other data collection then use the following command to start the server pointing to data directory located /var/lib/mongo.

[root@localhost mongo]# mongod --dbpath /var/lib/mongo

5. Open another Terminal window and type the following





3. Basic lab using MongoDB

Start MongoDB instance by following above steps.
In this section we will see basic CRUD operations and the small brief

  1. Show all available data bases in the Mongo
   
> show dbs
admin    (empty)
local    0.078GB

It will show the default dbs available in the instance.

2. Create new db

> use sampleDatabase
> show dbs
admin    (empty)
sampleDatabase 0.078GB

local    0.078GB

3. Creating new collection and inserting the data into collection
>db.data.insert({_id:1,stud_name:"suresh",score:40})
WriteResult({ "nInserted" : 1 })

The above command insert the data into collection called data.   Lets query it to see what is inserted to the collection

> db.data.find().pretty()

{ "_id" : 1, "stud_name" : "suresh", "score" : 90 }

If you see in the above command we are explicitly defining the _id:1. Suppose if you have not defined the _id, the mongo will take care of creating one for you, see the below example.

>db.data.insert({stud_name:"Rajesh",score:80})

WriteResult({ "nInserted" : 1 })

Query the collections

> db.data.find().pretty()
{ "_id" : 1, "stud_name" : "suresh", "score" : 90 }
{
        "_id" : ObjectId("54ccfe397fc420a23c8110e7"),
        "stud_name" : "Rajesh",
        "score" : 80

}

4. Show the collections in db sampleDatabase
> show collections
data

system.indexes

5. Update the existing document using update
> db.data.update({_id:1},{$set:{stud_name:"ramesh",score:71}},{upsert:true})

WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 1 })

Using update statement with upsert:true, the document is added to collection if it does not exist with _id:1

6. Insert a new document using “save()”
> db.data.save({_id:3,stud_name:"sachin",score:35})

WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 3 })

The above statement will create the new document in the collection if does not exist with _id:3, if the already exist then it will update the existing document

7. Finding all the documents created/updated till now

> db.data.find().pretty()
{ "_id" : 1, "stud_name" : "suresh", "score" : 90 }
{
        "_id" : ObjectId("54ccfe397fc420a23c8110e7"),
        "stud_name" : "Rajesh",
        "score" : 80
}
{ "_id" : 3, "stud_name" : "sachin", "score" : 35 }

8. Display all documents that match a specific condition

> db.data.find({stud_name:"sachin"})
{ "_id" : 3, "stud_name" : "sachin", "score" : 35 }

9. Display all documents that match a specific condition using operators (in).

>db.data.find({stud_name:{ $in:["suresh","sachin"]}})
{ "_id" : 1, "stud_name" : "sachin", "score" : 35 } { "_id" : 2, "stud_name" : "suresh", "score" : 90 }

10. Projections

In the above result set we are getting _id, stud_name, and score. Now suppose and we would like to see only stud_name and score from the collection.

> db.data.find({stud_name:"suresh"},{stud_name:1 , score:1, _id:0})

{ "stud_name" : "suresh", "score" : 90 }


11. Add new field to the existing document

> db.data.update({stud_name:"ramesh"},{$set:{"marks":10}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })


12. Finally we will delete a collection
> db.data.remove({marks:10})

13. Delete data base

> use sampleData
db.dropDatabase()