HOMEWORK: HOMEWORK 4.3 Solution to HOMEWORK 4.3 M101JS: MongoDB for Node.js Developers

NOTE there is a bug (TOOLS-939) affecting some versions of mongoimport and mongorestore that causes mongoimport -d blog -c posts < posts.json to fail. As a workaround, you can usemongoimport -d blog -c posts < posts.json --batchSize 1.If you have any difficulty using MongoProc, here are 2 video lectures showing how to set it up.

You will need to download one of the following versions of the MongoProc client, which will verify on your local machine (port 8082) that the signup and login pages of the blog work properly.

Download MongoProc

Making the Blog fastTo get started, please download hw4-3.zip from the Download Handout link and unpack file to your computer. This assignment requires Mongo 2.2 or above.

In this homework assignment you will be adding some indexes to the post collection to make the blog fast.

We have provided the full code for the blog application and you don’t need to make any changes, or even run the blog. But you can, for fun.

We are also providing a patriotic (if you are an American) data set for the blog. There are 1000 entries with lots of comments and tags. You must load this dataset to complete the problem.

# from the mongo shell
use blog
# from the a mac or PC terminal window
mongoimport -d blog -c posts < posts.json

The blog has been enhanced so that it can also display the top 10 most recent posts by tag. There are hyperlinks from the post tags to the page that displays the 10 most recent blog entries for that tag. (run the blog and it will be obvious)

Your assignment is to make the following blog pages fast:

  • The blog home page
  • The page that displays blog posts by tag (http://localhost:8082/tag/whatever)
  • The page that displays a blog entry by permalink (http://localhost:8082/post/permalink)

By fast, we mean that indexes should be in place to satisfy these queries such that we only need to scan the number of documents we are going to return.

To figure out what queries you need to optimize, you can read the code in posts.js and see what queries it is doing to return the data needed for the relevant pages. Isolate those queries and use explain to explore.

Once you have added the indexes to make those pages fast, just validate your work with MongoProc.

You have used 1 of 3 submissions.

Run following queries one by one and submit it.
db.posts.ensureIndex({ date : -1})
db.posts.find({date : -1}).limit(10).explain()
db.posts.ensureIndex({ tags : 1, date : -1})
db.posts.find({tags : 'vietnam'}).sort({ date : -1}).explain()
db.posts.ensureIndex({ permalink : 1})
db.posts.find({permalink: "xnafqemtzzcyyzjdegkj"}).explain()




Please enter your comment!
Please enter your name here