Building Application with Mongodb

For the past few months I was diving deep into mongodb for an application whose back-end is entirely supported by mongo.The word mongo was first introduced to me by one of my teacher Sudarsun and having got interest I invited my self to explore more about it. As a result I ended up with developing an application with a back-end supported by mongodb.Since it was a blogging application my focus was more on database side rather than GUI part. And I hereby document my techniques and difficulties that I have faced during my db design.

Before starting I have to fix my requirement and I kept it to be very simple; that is  A user can have a blog , a user can have more than a blog and each blog can have as many posts as it can.

Pictorial representation of blog requirement

Pictorial representation of blog requirement

Keeping the requirements in mind my design progress started without any assistants. Since I had an idea to use mongoDB I have to design my schema in such way that it is not again falling into relational db paradigms that is I should not segregate blogs , post , comments in separate collections (tables) possibly by some defined relational keys. Instead I need to categorize them together as single group and see to that there is no redundancy of data occurred. (This was challenging really but still I was able to achieve what I was looking for).

The secrete for all MYSQL forks who has a habit of segregating the tables , can do the same with sub-document feature of mongoDB. This will not create any discrepancy with main document in fact querying in sub-document is much faster than on your associated table that is segregated before. However writing query to retrieve the targeted data from a sub-document would be challenging again so the easier way to do is to refer the sub-document by its immediate parent document. I also recommend to use arrays along with sub-document where retrieval of data is required in groups (example pagination )

Finally here is my schema that satisfies all the above mentioned requirements.

{
    "blog_count":2,
    "blogs":{
        "1":{
            "blog_id":1,
            "date_time":"Oct 26 2011 10:15:46 AM",
            "draft_count":0,
            "post_count":2,
            "posts":{
                "1":{
                    "comment_count":2,
                    "comments":[{
                            "name":"shakthydoss",
                            "email_id":"shakthydoss@gmail.com",
                            "website":"../../usr/user.php?usr_id=1027",
                            "comment":"This is my comment.... ",
                            "date":"2011-10-26 04:53:03"
                        },
                        {
                            "name":"shakthydoss",
                            "email_id":"shakthydoss@gmail.com",
                            "website":"../../usr/user.php?usr_id=1027",
                            "comment":"This is a good post ",
                            "date":"2011-10-26 04:53:25"
                        }
                    ],
                    "post_content":"Hello world !i am happy to see you",
                    "post_data_time":"Oct 26 2011 10:17:12 AM",
                    "post_file_name":"my-post-1.php",
                    "post_id":1,
                    "post_title":"My Post 1"
                },
                "2":{
                    "post_id":2,
                    "post_data_time":"Oct 26 2011 10:17:49 AM",
                    "post_file_name":"my-post-2.php",
                    "post_title":"My Post 2",
                    "post_content":"This is cool"
                }
            },
            "url":"blogs/shakthydoss/"
        },
        "2":{
            "blog_id":2,
            "date_time":"Oct 26 2011 10:18:15 AM",
            "draft_count":1,
            "drafts":{
                "1":{
                    "draft_id":1,
                    "post_data_time":"Oct 26 2011 10:20:18 AM",
                    "post_id":-1,
                    "post_title":"My draft 1",
                    "post_content":"This is my draft"
                }
            },
            "post_count":1,
            "posts":{
                "1":{
                    "post_id":1,
                    "post_data_time":"Oct 26 2011 10:18:52 AM",
                    "post_file_name":"my-scrap.php",
                    "post_title":"My scrap..",
                    "post_content":"This is my scrap..."
                }
            },
            "url":"blogs/abc-abc/"
        }
    },
    "total_post_count":0,
    "user_id":1027
} so whats next..? its big day for me .....  

Big Day for Me

what shall I do after developing such application , should I have to add this to my project completed list or put the apps to my achieve . No I am going to do something else and I have scheduled it to be on October 29 2011. So stay tuned till then and I will let u know what I have done exactly ……

October 29 2011

October 29 2011

 


Post a Comment

Your email is never published nor shared. Required fields are marked *