M101p Mongodb For Developers Homework Helper

[Solution] Week 2 : CRUD : M101P: MongoDB for Developers

Homework 2.1 : 

In this problem, you will be using a collection of student scores that is similar to what we used in the lessons. Please download grades.json from the Download Handout link and import it into your local mongo database as follows:

mongoimport --drop -d students -c grades grades.json

The dataset contains 4 scores for 200 students.

First, let's confirm your data is intact; the number of documents should be 800.

use students db.grades.count() You should get 800.


This next query, which uses the aggregation framework that we have not taught yet, will tell you the student_id with the highest average score:

db.grades.aggregate({'$group':{'_id':'$student_id', 'average':{$avg:'$score'}}}, {'$sort':{'average':-1}}, {'$limit':1})

The answer should be student_id 164 with an average of approximately 89.3.

Now it's your turn to analyze the data set. Find all exam scores greater than or equal to 65, and sort those scores from lowest to highest.

What is the student_id of the lowest exam score above 65?



Solution :

    Query : db.grades.find( { score : { $gte : 65 } } ).sort( { score : 1 } )

    Answer : 22

Homework 2.2 : 

Write a program in the language of your choice that will remove the grade of type "homework" with the lowest score for each student from the dataset in the handout. Since each document is one grade, it should remove one document per student. This will use the same data set as the last problem, but if you don't have it, you can download and re-import.

The dataset contains 4 scores each for 200 students.

First, let's confirm your data is intact; the number of documents should be 800.

use students db.grades.count() Hint/spoiler: If you select homework grade-documents, sort by student and then by score, you can iterate through and find the lowest score for each student by noticing a change in student id. As you notice that change of student_id, remove the document.

To confirm you are on the right track, here are some queries to run after you process the data and put it into the grades collection:

Let us count the number of grades we have:


db.grades.count() The result should be 600. Now let us find the student who holds the 101st best grade across all grades:

db.grades.find().sort( { 'score' : -1 } ).skip( 100 ).limit( 1 )
The correct result will be:
{ "_id" : ObjectId("50906d7fa3c412bb040eb709"), "student_id" : 100, "type" : "homework", "score" : 88.50425479139126 }

Now let us sort the students by student_id , and score, while also displaying the type to then see what the top five docs are:

db.grades.find( { }, { 'student_id' : 1, 'type' : 1, 'score' : 1, '_id' : 0 } ).sort( { 'student_id' : 1, 'score' : 1, } ).limit( 5 )

The result set should be:

{ "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 } { "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 } { "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 } { "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 } { "student_id" : 1, "type" : "exam", "score" : 74.20010837299897 }
To verify that you have completed this task correctly, provide the identity of the student with the highest average in the class with following query that uses the aggregation framework. The answer will appear in the _id field of the resulting document.

db.grades.aggregate( { '$group' : { '_id' : '$student_id', 'average' : { $avg : '$score' } } }, { '$sort' : { 'average' : -1 } }, { '$limit' : 1 } ) Enter the student ID below. Please enter just the number, with no spaces, commas or other characters.

Solution :    Answer : 54


Homework 2.3 : 

Blog User Sign-up and Login

Download the handout and unpack it.

You should see three files at the highest level: blog.py, userDAO.py and sessionDAO.py. There is also a views directory which contains the templates for the project.

The project roughly follows the model/view/controller paradigm. userDAO and sessionDAO.py comprise the model. blog.py is the controller. The templates comprise the view.

If everything is working properly, you should be able to start the blog by typing:

python blog.py Note that this project requires the following python modules be installed on your computer: cgi, hmac, datetime, json, sys, string, hashlib, urllib, urllib2, random, re, pymongo, and bottle. If you have python installed at all, you probably already have most of these installed except pymongo and bottle.

If you have python-setuptools installed, the command "pip" makes this simple. Any other missing packages will show up when validate.py is run, and can be installed in a similar fashion. As of this recording, we are still using the beta version of PyMongo so we will install directly from GitHub. Note that this directions are identical to what we taught you within the lessons and you should already have PyMongo 3.x and bottle installed. If not, use the following commands.s.

First, let's confirm your data is intact; the number of documents should be 800.

$ pip install https://github.com/mongodb/mongo-python-driver/archive/3.0b1.tar.gz $ pip install bottleIf you go to http://localhost:8082 you should see a message, "this is a placeholder for the blog"

Here are some URLs that must work when you are done.


http://localhost:8082/signup http://localhost:8082/login http://localhost:8082/logout

This next query, which uses the aggregation framework that we have not taught yet, will tell you the student_id with the highest average score:

Solution :  jkfds5834j98fnm39njf0920f02


Homework 2.4 :

Which of the choices below is the title of a movie from the year 2013 that is rated PG-13 and won no awards? Please query the video.movieDetails collection to find the answer.

NOTE: There is a dump of the video database included in the handouts for the "Creating Documents" lesson. Use that data set to answer this question.



Homework 2.5 :

Using the video.movieDetails collection, how many movies list "Sweden" second in the the list of countries.

NOTE: There is a dump of the video database included in the handouts for the "Creating Documents" lesson. Use that data set to answer this question.



Enjoy....!!!!

Feel free to comment below your experience with above approach and If you still find any problem  with above steps Let me know I would love to help you to resolve your  problem.

 [Solution] Week 4 :Performance : M101P: MongoDB for Developers

Homework 4.1 : 

Suppose you have a collection with the following indexes:

> db.products.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "store.products", "name" : "_id_" }, { "v" : 1, "key" : { "sku" : 1 }, "unique" : true, "ns" : "store.products", "name" : "sku_1" }, { "v" : 1, "key" : { "price" : -1 }, "ns" : "store.products", "name" : "price_-1" }, { "v" : 1, "key" : { "description" : 1 }, "ns" : "store.products", "name" : "description_1" }, { "v" : 1, "key" : { "category" : 1, "brand" : 1 }, "ns" : "store.products", "name" : "category_1_brand_1" }, { "v" : 1, "key" : { "reviews.author" : 1 }, "ns" : "store.products", "name" : "reviews.author_1" } ]

Which of the following queries can utilize at least one index to find all matching documents, or to sort? Check all that apply.

Note: the text for some answers may wrap; you can ignore the wrapping.



Homework 4.2 : 

Suppose you have a collection called tweets whose documents contain information about the created_at time of the tweet and the user's followers_count at the time they issued the tweet. What can you infer from the following explain output?

> db.tweets.explain("executionStats").find( { "user.followers_count" : { $gt : 1000 } } ).limit(10).skip(5000).sort( { created_at : 1 } ) { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "twitter.tweets", "indexFilterSet" : false, "parsedQuery" : { "user.followers_count" : { "$gt" : 1000 } }, "winningPlan" : { "stage" : "LIMIT", "limitAmount" : 0, "inputStage" : { "stage" : "SKIP", "skipAmount" : 0, "inputStage" : { "stage" : "FETCH", "filter" : { "user.followers_count" : { "$gt" : 1000 } }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "created_at" : -1 }, "indexName" : "created_at_-1", "isMultiKey" : false, "direction" : "backward", "indexBounds" : { "created_at" : [ "[MinKey, MaxKey]" ] } } } } }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 10, "executionTimeMillis" : 563, "totalKeysExamined" : 251120, "totalDocsExamined" : 251120, "executionStages" : { "stage" : "LIMIT", "nReturned" : 10, "executionTimeMillisEstimate" : 500, "works" : 251121, "advanced" : 10, "needTime" : 251110, "needFetch" : 0, "saveState" : 1961, "restoreState" : 1961, "isEOF" : 1, "invalidates" : 0, "limitAmount" : 0, "inputStage" : { "stage" : "SKIP", "nReturned" : 10, "executionTimeMillisEstimate" : 500, "works" : 251120, "advanced" : 10, "needTime" : 251110, "needFetch" : 0, "saveState" : 1961, "restoreState" : 1961, "isEOF" : 0, "invalidates" : 0, "skipAmount" : 0, "inputStage" : { "stage" : "FETCH", "filter" : { "user.followers_count" : { "$gt" : 1000 } }, "nReturned" : 5010, "executionTimeMillisEstimate" : 490, "works" : 251120, "advanced" : 5010, "needTime" : 246110, "needFetch" : 0, "saveState" : 1961, "restoreState" : 1961, "isEOF" : 0, "invalidates" : 0, "docsExamined" : 251120, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 251120, "executionTimeMillisEstimate" : 100, "works" : 251120, "advanced" : 251120, "needTime" : 0, "needFetch" : 0, "saveState" : 1961, "restoreState" : 1961, "isEOF" : 0, "invalidates" : 0, "keyPattern" : { "created_at" : -1 }, "indexName" : "created_at_-1", "isMultiKey" : false, "direction" : "backward", "indexBounds" : { "created_at" : [ "[MinKey, MaxKey]" ] }, "keysExamined" : 251120, "dupsTested" : 0, "dupsDropped" : 0, "seenInvalidated" : 0, "matchTested" : 0 } } } } }, "serverInfo" : { "host" : "generic-name.local", "port" : 27017, "version" : "3.0.1", "gitVersion" : "534b5a3f9d10f00cd27737fbcd951032248b5952" }, "ok" : 1 }

Homework 4.3 :

use blog db.posts.drop()
From the mac or PC terminal window
mongoimport --drop -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 blog.py code and see what it does to display those pages. Isolate those queries and use explain to explore.

Once you have added the indexes to make those pages fast run the following

python validate.py

(note that for folks who are using MongoLabs or MongoHQ there are some command line options to validate.py to make it possible to use those services) Now enter the validation code below.

Making the Blog fast
Please download hw4-3.zip from the Download Handout link to get started. This assignment requires Mongo 3.0 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:

Solution : 893jfns29f728fn29f20f2


Homework 4.4 :

In this problem you will analyze a profile log taken from a different mongoDB instance and you will import it into a collection named sysprofile. To start, please download sysprofile.json from Download Handout link and import it with the following command:

mongoimport --drop -d m101 -c profile sysprofile.json
Now query the profile data, looking for all queries to the students collection in the database school2, sorted in order of decreasing latency. What is the latency of the longest running operation to the collection, in milliseconds?

Solution :



Enjoy....!!!!

Feel free to comment below your experience with above approach and If you still find any problem  with above steps Let me know I would love to help you to resolve your  problem.

 If you want to take your Technological Knowledge to the Next Level and For More Technological information Stay tuned to Visionfortech


0 thoughts on “M101p Mongodb For Developers Homework Helper”

    -->

Leave a Comment

Your email address will not be published. Required fields are marked *