You are on page 1of 3

(/)

Courses (/courses) Help (/help) About (/about) Blog (http://ed-blog.mongodb.com/) DMAKETAS (/DASHBOARD)

M101J: MongoDB for Java Developers


Courseware (/courses/10gen/M101J/2013_October/courseware) Discussion (/courses/10gen/M101J/2013_October/discussion/forum) Progress (/courses/10gen/M101J/2013_October/progress) Course Info (/courses/10gen/M101J/2013_October/info) Wiki (/courses/10gen/M101J/2013_October/course_wiki)

SEARCH

Homework: HW 3.1
Download the students.json (/static/m101j-october-2013/handouts/students.432aefc2cf4e.json) file to your computer and import it into your local Mongo instance with this command:
$m o n g o i m p o r tds c h o o lcs t u d e n t s<s t u d e n t s . j s o n

This dataset holds the same type of data as last week's grade collection, but it's modeled differently. You might want to start by inspecting it in the Mongo shell. Write a program in Java that will remove the lowest homework score for each student. Since there is a single document for each student containing an array of scores, you will need to update the scores array and remove the homework. Hint/spoiler: With the new schema, this problem is a lot harder and that is sort of the point. One way is to find the lowest homework in code and then update the scores array with the low homework pruned. If you are struggling with the Java side of this, look at the ArrayList.remove method, which can remove stuff from a Java ArrayList. Note also that when the value for a particular key is list within MongoDB, the Java driver returns a BasicDBList, which can you cast to an ArrayList. To confirm you are on the right track, here are some queries to run after you process the data with the correct answer shown: Let us count the number of students we have:

>u s es c h o o l >d b . s t u d e n t s . c o u n t ( ) 2 0 0

Let's see what Demarcus Audette's record looks like:


> d b . s t u d e n t s . f i n d ( { _ i d : 1 0 0 } ) . p r e t t y ( ) { " _ i d ":1 0 0 , " n a m e ":" D e m a r c u sA u d e t t e " , " s c o r e s ":[ { " t y p e ":" e x a m " , " s c o r e ":3 0 . 6 1 7 4 0 6 4 0 6 3 6 8 7 1 } , { " t y p e ":" q u i z " ,

" t y p e ":" q u i z " , " s c o r e ":1 4 . 2 3 2 3 3 8 2 1 3 5 3 7 3 2 2 } , { " t y p e ":" h o m e w o r k " , " s c o r e ":3 1 . 4 1 4 2 1 2 9 8 5 7 6 3 3 2 4 } ] }

To verify that you have completed this task correctly, provide the identify 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.
>d b . s t u d e n t s . a g g r e g a t e ( { ' $ u n w i n d ' : ' $ s c o r e s ' } , { ' $ g r o u p ' : { ' _ i d ' : ' $ _ i d ' ,' a v e r a g e ' : { $ a v g : ' $ s c o r e s . s c o r e ' } } } , { ' $ s o r t ' : { ' a v e r a g e ' : 1 } } ,{ ' $ l i m i t ' : 1 } )

Enter just the numeric value of the _id below.

37 HIDE ANSWER You have used 0 of 3 submissions

Products & Services (http://www.mongodb.com/products/mongodb) Events (http://www.mongodb.com/events) Partners (http://www.mongodb.com/partners) Customers (http://www.mongodb.com/customers) About MongoDB (http://www.mongodb.com/about) Contact Us (/contact) Careers (http://www.mongodb.com/careers)

(https://twitter.com/mongodbinc)

(http://www.facebook.com/mongodb)

(http://www.linkedin.com/company/783611)

(https://plus.google.com/101024085748034940765/posts) (http://edx.org) (http://www.mongodb.com)


2013 MongoDB, Inc. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB Inc.

You might also like