Professional Documents
Culture Documents
Courses (/courses) Help (/help) About (/about) Blog (http://ed-blog.mongodb.com/) DMAKETAS (/DASHBOARD)
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
" 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 } )
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)