Mongodb University Homework

[Solution] Week 3 :The Node.js Driver : M101JS: MongoDB for Node.js Developers

 [Solution] Week 3 :The Node.js Driver  : M101JS: MongoDB for Node.js Developers

Homework 3.1 : 

When using find() in the Node.js driver, which of the following best describes when the driver will send a query to MongoDB?

Solution :




Homework 3.2 : 

Suppose you have a MongoDB collection called school.grades that is composed solely of these 20 documents:
{"_id": 1, "student": "Mary", "grade": 45, "assignment": "homework"} {"_id": 2, "student": "Alice", "grade": 48, "assignment": "homework"} {"_id": 3, "student": "Fiona", "grade": 16, "assignment": "quiz"} {"_id": 4, "student": "Wendy", "grade": 12, "assignment": "homework"} {"_id": 5, "student": "Samantha", "grade": 82, "assignment": "homework"} {"_id": 6, "student": "Fay", "grade": 89, "assignment": "quiz"} {"_id": 7, "student": "Katherine", "grade": 77, "assignment": "quiz"} {"_id": 8, "student": "Stacy", "grade": 73, "assignment": "quiz"} {"_id": 9, "student": "Sam", "grade": 61, "assignment": "homework"} {"_id": 10, "student": "Tom", "grade": 67, "assignment": "exam"} {"_id": 11, "student": "Ted", "grade": 52, "assignment": "exam"} {"_id": 12, "student": "Bill", "grade": 59, "assignment": "exam"} {"_id": 13, "student": "Bob", "grade": 37, "assignment": "exam"} {"_id": 14, "student": "Seamus", "grade": 33, "assignment": "exam"} {"_id": 15, "student": "Kim", "grade": 28, "assignment": "quiz"} {"_id": 16, "student": "Sacha", "grade": 23, "assignment": "quiz"} {"_id": 17, "student": "David", "grade": 5, "assignment": "exam"} {"_id": 18, "student": "Steve", "grade": 9, "assignment": "homework"} {"_id": 19, "student": "Burt", "grade": 90, "assignment": "quiz"} {"_id": 20, "student": "Stan", "grade": 92, "assignment": "exam"} Assuming the variable db holds a connection to the school database in the following code snippet.

var cursor = db.collection("grades").find({}); cursor.skip(6); cursor.limit(2); cursor.sort({"grade": 1}); Which student's documents will be returned as part of a subsequent call to toArray()?

Solution :
       Answer : Seamus,Bob












Homework 3.3 :

This application depends on the companies.json dataset distributed as a handout with the findAndCursorsInNodeJSDriver lesson. You must first import that collection. Please ensure you are working with an unmodified version of the collection before beginning this exercise.

To import a fresh version of the companies.json data, please type the following:

mongoimport --drop -d crunchbase -c companies companies.json
If you have already mongoimported this data you will first need to drop the crunchbase database in the Mongo shell. Do that by typing the following two commands, one at a time, in the Mongo shell:

use crunchbase db.dropDatabase()
The code in the attached handout is complete with the exception of the queryDocument() function. As in the lessons, the queryDocument() function builds an object that will be passed to find() to match a set of documents from the crunchbase.companies collection.

For this assignment, please complete the queryDocument() function as described in the TODO comments you will find in that function.

Once complete, run this application by typing:

node buildingQueryDocuments.js
When you are convinced you have completed the application correctly, please enter the average number of employees per company reported in the output. Enter only the number reported. It should be three numeric digits.

As a check that you have completed the exercise correctly, the total number of unique companies reported by the application should equal 42.

Solution :

function queryDocument(options) { var query = { "tag_list": {"$regex": "social-networking"}, }; if (("firstYear" in options) && ("lastYear" in options)) { query.founded_year = { "$gte": options.firstYear, "$lte": options.lastYear }; } else if ("firstYear" in options) { query.founded_year = { "$gte": options.firstYear }; } else if ("lastYear" in options) { query.founded_year = { "$lte": options.lastYear }; } if ("city" in options) { query["offices.city"] = options.city; } return query; }     Answer : 169















Homework 3.4 :


In completing this exercise, you will find the "Logical Operators" lesson from Chapter 2 of this course helpful as a refresher on the $or operator.

This application depends on the companies.json dataset distributed as a handout with the "find() and Cursors in the Node.js Driver" lesson. You must first import that collection. Please ensure you are working with an unmodified version of the collection before beginning this exercise.

To import a fresh version of the companies.json data, please type the following:

mongoimport --drop -d crunchbase -c companies companies.json If you have already mongoimported this data you will first need to drop the crunchbase database in the Mongo shell. Do that by typing the following two commands, one at a time, in the Mongo shell:

use crunchbase db.dropDatabase() The code attached is complete with the exception of the queryDocument() function. As in the lessons, the queryDocument() function builds an object that will be passed to find() to match a set of documents from the crunchbase.companies collection.

For this assignment, please complete the queryDocument() function as described in the TODO comments you will find in that function.

Once complete, run this application by typing:

node overviewOrTags.jsWhen you are convinced you have completed the application correctly, please enter the average number of employees per company reported in the output. Enter only the number reported. It should be two numeric digits.

As a check that you have completed the exercise correctly, the total number of unique companies reported by the application should equal 194.

If the grading system does not accept the first solution you enter, please do not make further attempts to have your solution graded without seeking some help in the discussion forum.

Solution :

function queryDocument(options) { var query = {}; if ("overview" in options) { query["$or"] = [ {"overview": {"$regex": options.overview, "$options": "i"}}, {"tag_list": {"$regex": options.overview, "$options": "i"}} ]; } if ("milestones" in options) { query["milestones.source_description"] = {"$regex": options.milestones, "$options": "i"}; } return query; }     Answer : 48

















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

Related Post : 

1) [SOLUTION] WEEK 1 : INTRODUCTION : M101JS: MongoDB for Node.js Developers

2) [SOLUTION] WEEK 2 : CRUD : M101JS: MongoDB for Node.js Developers

3) [SOLUTION] Week 3 :The Node.js Driver : M101JS: MongoDB for Node.js Developers

4) [SOLUTION] WEEK 4 : SCHEMA DESIGN : M101JS: MongoDB for Node.js Developers

5) [SOLUTION] WEEK 5 : INDEXES AND PERFORMANCE : M101JS: MongoDB for Node.js Developers

6) [SOLUTION] WEEK 6 : THE AGGREGATION FRAMEWORK : MongoDB for Node.js Developers

7) [SOLUTION] WEEK 7 :APPLICATION ENGINEERING : MongoDB for Node.js Developers

8) [SOLUTION] FINAL EXAM(PROJECT) :MONGOMART APPLICATION : M101JS: MongoDB for Node.js Developers

[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.

0 thoughts on “Mongodb University Homework”

    -->

Leave a Comment

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