Thursday, April 22, 2010

GridFS in MongoDB

I was trying to store files in MongoDB. As was new to MongoDB, I had to seek help from Google. Finally, I came to know about GridFS and GridStore. Gave a kick start to them and worked out some samples using GridStore.

To my surprise I found out that GrisdFS using two collections, viz., files and chunks which was completely unfamiliar. After digging deep I came to know that files are one that contains the actual data and the chunks are the one that contains the binary chunks of that file. Each file stored will have a unique file ID.


To store a file using GridStore,



To retrieve a file using GridStore

Having  toiled away in getting this through all day, I was relaxing by browsing  more on GridStore to my surprise, fortunately or unfortunately I came to know that the GridStore is deprecated from driver version 0.19. GridStore was replaced by two new classes, viz., Grid and GridFileSystem.

Grid allows you to store and retrieve files using unique ID and GridFileSystem uses the filename.

File operations using Grid


File operations using GridFileSystem

Eventually, I found it really coooooool.

Friday, March 26, 2010

MapReduce - Really reduces!

Look at the image below
Image and video hosting by TinyPic

Yes, absolutely they are the labels of the blog posts. Do you have any idea how the labels are grouped together along with their counts? Which is commonly known as batch manipulation and aggregation of data.

If I'm a relational database user my answer would be like this,
  • Have a table for tags.

  • Have a table for posts.

  • Associate the related tags to the posts.

  • Do some complex queries using 'GROUP BY' to fetch the tags and their counts.

But in the case of document oriented database like MongoDB, the tags will be stored as a String Array inside each post object. For example a post objects will look like





As you see, tags are not related to each other and two post objects can have redundant tags but, how do we group these tags?

It is a piece of cake through a tool called MapReduce. As the name indicates this includes two functions, Map and Reduce. To simplify we can say as,


Map function - emits each tag as a key, value pair.




In the above snippet, this refers to the current post object and the tags refers to the array of tags for that post.

Reduce function - receives the key and the value array, reduces the received values and eventually returns the result.







For more detailed info about these functions have a look at http://www.mongodb.org/display/DOCS/MapReduce.

Now let us see a sample code for illustration. The scenario is the same, there will be list of tasks and each task will have n number of tags and we have to group these tags with their count.

My tasks collection will look like the snippet below




The MapReduce class is written as,




Note : Here I had used MongoMapper, Mongo ORM for Ruby. To know more about MongoMapper, visit http://mongomapper.com/

You can also use, db = Mongo::Connection.new("localhost", 27017).db("mydb")

Used this Class to mapReduce tags as



Wednesday, March 24, 2010

Kick Start

This is my first blog. Here I want to share the reason which pulled me to write blog. I used to update myself with the latest technologies (TechGraze!) and while doing so I found a fact that most of the perfect solutions I got were only through various blogs than documentations. Hence with the motivation of my colleagues, I thought of blogging the new things I learn and I use, though it may be a simple thought or information.

I started working on Ruby and MongoDB only a month ago, they sound like Greek and Latin to me at first. But when get started, I find them amazing. Previously I used to develop web applications using Java and MySQL. But I find Ruby more simpler and easier than Java. The most important fact is that the development speed in Ruby is awesome.


Most of the people think that the databases are always meant to be relational, even I was thinking the same a month ago but now things have changed. Introduction to MongoDB had changed my views towards databases. Document oriented database structure is something new for me and weird when I heard first. But when I started using it, I found it more simple and I jus WOW. No need for lengthy sql queries. For instance, instead of "select *" query we can use "find()".

I find myself very interested and enthusiastic in learning these technologies which are completely new for me. I used to love Java but "I got a better girl"!