Tuesday, August 19, 2014

M101J: MongoDB for Java Developers Homework 3.1

Step 1:
Download students.json from download handout

Step 2:
Import the same using mongo import
mongoimport -d school -c students < students.json

Step 3:
Write a program in your language to delete that will remove the lowest homework score for each student.

Step 4:
Cross check your data

Check 1:
Go to mongo shell
type
> use school
> db.students.count();
200


Check 2:
> db.students.find({_id:100}).pretty();
{
        "_id" : 100,
        "name" : "Demarcus Audette",
        "scores" : [
                {
                        "type" : "exam",
                        "score" : 47.42608580155614
                },
                {
                        "type" : "quiz",
                        "score" : 44.83416623719906
                },
                {
                        "type" : "homework",
                        "score" : 19.85604968544429
                },
                {
                        "type" : "homework",
                        "score" : 39.01726616178844
                }
        ]
}


Check 3:
Aggregation Query for student with highest average

db.students.aggregate([{'$unwind':'$scores'},{'$group':{'_id':'$_id','average':{$avg:'$scores.score'}}},{'$sort':{'average':-1}},{'$limit':1}])


Next homework will do it tomorrow.




No comments:

Post a Comment