Vous êtes sur la page 1sur 4

M101N: MongoDB for .

NET Developers

1.
Download Handouts:

grades.json

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?

Choose the best answer:

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.
Download Handouts:

M101N_registration_and_login__55105a5fd8ca39285577dfe3.zip

Homework: Homework 2.3 (MongoProc): Blog User


Registration and Login
Download Mongoproc clients from here if you haven't done so, yet.

You may need to spend some time setting it up.

Login with your email/password combination that you use for university.mongodb.com.
When you log in, you can see a 'settings' button. Click on it to set host:port values.
Your blog should be hosted on localhost:57912, and mongoProc is not pointed there by
default.
Your mongod is on port 27017 by default, and mongoProc should point there initially;
when you change it, it should stick.

Also, download the handout from the Download Handout link above, and unpack it.

This is the beginning of the blog project with the UI for creating and logging in blog authors, but
nothing to display posts.

The project uses ASP.NET MVC5. You should open the solution file (.sln) in Visual Studio and
recognize a typical project. We have used the included template and haven't pulled in any extra
dependencies.

The project won't compile immediately as there are missing pieces you'll need to fill out. These
pieces are marked with XXX in a comment. You shouldn't need to touch any other code. First, in
User.cs, you'll need to add in the contents of the User object. Second, in AccountController,
you'll need to insert a user and retrieve a user by email.

The blog stores its data in the "blog" database in the "users" collection. Here are two example
documents from the users collection. You can insert these if you'd like, but you don't need to.

> db.users.find()
{ "_id" : ObjectId("54c272a9122ea91adc328696"), "Name" : "Craig", "Email" :
"craig@craig.com" }
{ "_id" : ObjectId("54c27f50122ea914546658be"), "Name" : "Andrew", "Email" :
"andrews@andrew.com" }

Once you have the project working, the following steps should work.

Click "Register" on the home page and register a user.


Click "Login" on login with the user you just created.
Ok, now it's time to validate that you got it all working.

From the top of this page, there was one additional program that should have been downloaded:
mongoProc.

With it, you can test your code and look at the Feedback section. When it says "user creation
successful" and "user login successful", you can turn in your assignment.

You will see a message below about your number of submissions, but you must submit this
assignment using MongoProc.

You have used 0 of 3 submissions.

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.

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.

Vous aimerez peut-être aussi