Google+ Badge

Sunday, 10 July 2016

NoSQL : Essentials and Short tutorial on Mongodb - Part 2

In this post , I am going to present a short and precise tutorial on Mongodb and it's important commands and it's advance features.

Documents and Collections 

Unlike the RDMS, Mongodb lacks rows, columns, tables, joins. Instead Mongodb employs documents and collections. Documents are name\value pairs and can store array of values, to present a clear picture , consider documents as rows and collections as tables when compared to traditional database.Collections store documents. Now let me create a document,
name: "uden"
The below example shows how to create document using Java:
DBObject owner=new                       BasicDBObject("name","uden") .append("type","four-wheeler")
Documents could have nested documents, here company is the nested document within the vehicle document.

Performing Insert,Update,Delete and Query 

Let us see how to create a database and insert,update,
read,delete operations.Creating a document is discussed in the previous section.Creating database is done by using 
use udendb ,where udendb is the database name which I will use in this tutorial, you can name the database in whatever name you desire.
Now let us insert a document into the database using insert() command,
db.col.insert({"vehicle"} , here col is collection name where Mongodb creates Collections by default.
You can also add several documents by passing it as arrays.After inserting a document into the database, you can check the database by using,
show dbs 
There will be a default database know as test which stores collections if you don't wish to create a database.
Update: In order to update values, update() method is used, consider the following example,
here col is the default collection name and I have updated my company from ferrai to mahindra and if you want to update value to multiple documents, then you need to set multi to true, in my example I have multi to false since I want only to update a single document.
Query: To query a document, you can use pretty() method along with find() which gives results in a structured way, for example:
db.col.find(). pretty()
You can also find according to criteria or some condition by using AND denoted by , (comma) between the values and by using OR denoted by $or.
Here the results will display vehicle in yellow and whose company  is either mahindra or by ferrai.
Delete: Delete operation is a simple task which can be achieved by using,
remove() which removes all documents.
remove(name of desired document to be deleted) which removes according to instruction give within in the parenthesis.

Using GridFS

GridFS is a file system which can be used for retrieving and storing files such as images , videos etc. It is mainly used to store large size of files more than more than 16 mb. 

fs.files is used to store metadata in files.

fs.chunks is used to store chunks , where each chunk is given objectID.

Now I will show you how to add an image file using GridFS,
mongofiles.exe -d gridfs put image.jpg
This command is typed in the mongofiles.exe in the bin directory, GridFS is the database name and put command stores the image file in gridfa database.

Mongodb Mapreduce 
Mapreduce is a large data processing tool which is also supported by Mongodb.The syntax command is,
mapreduce( function() {emit(key,value);}
                       {return reduceFunction},{
                        out: collection, query: document,
                         sort: document, limit:number } }
Let me explain with an example,I am going to collect all cars which are yellow in colour and group them under ferrai company and then count number of cars manufactured by ferrai.. Consider the document created in the first section (vehicle). Now the mapreduce would be,
mapreduce( function() {emit(ferrai,1);)}
                        function((key,values){return Array.sum(values)},
 out:"total ferrai cars"})
The result will be,
{ results : "total ferrai cars"
The result shows 18 documes matched the query yellow and emitted 3 accoridmg to key value and finally reduce function grouped same values into 2.Hence there are two cars manufactured by ferrai.

Mongodb Text search
Mongodb Text search enables to search specified words.Let me show you how to search text with help of an example,
Consider the document vehicle which was created in the first section,I want to search for ferrai word under the nested document company. First you need to create text index by using this command,
Now we can search the text using,
db. vehicle.find({$text:{$search:"ferrai"})

Conclusion: I covered the basics commands and some of its advanced features, I provided the links below for resources if you want detailed information.

  • For more on create ,read,update and bulk write
  • To download Mongodb
Useful books to learn