Monday, September 1, 2014

M101J: MongoDB for Java Developers Homework 4.3

M101J: MongoDB for Java Developers Homework 4.3:

Step 1: Download Handout 

Step 2:
Problem Statement: we need to make blog fast by adding index to post collection.

Before that you need to import post.json

Steps to import:
from mongo shell 
>use blog
>db.posts.drop()
from the terminal window, you can go to uncompress directory of homework
and try below query to import.
mongoimport -d blog -c posts < posts.json


now, we need to improve performance for
1) Blogs Home page.
2)The page that displays blog posts by tag (http://localhost:8082/tag/whatever)
3) The page that displays a blog entry by permalink (http://localhost:8082/post/permalink)


1) For Query DBCursor cursor = postsCollection.find().sort(new BasicDBObject().append("date", -1)).limit(limit);
 

You can add
db.posts.ensureIndex({ date: -1})

2) for         
DBObject post = postsCollection.findOne(new BasicDBObject("permalink", permalink));

you can add index on 
db.posts.ensureIndex({ permalink: 1}, {unique: true})
 BasicDBObject query = new BasicDBObject("tags", tag);
     
   System.out.println("/tag query: " + query.toString());
        

DBCursor cursor = postsCollection.find(query).sort(new BasicDBObject().append("date", -1)).limit(10);


you can add index on 

 db.posts.ensureIndex({ tags: 1})

and then submit you answer in mongo proc.

No comments:

Post a Comment