Friday, 8 September 2017

Week 5 : AGGREGATION FRAMEWORK : M101P: MongoDB for Developers

Homework 5.1 : 


In this assignment you will use the aggregation framework to find the most frequent author of comments on your blog. We will be using a data set similar to ones we've used before.

Start by downloading the handout zip file for this problem. Then import into your blog database as follows:
mongoimport --drop -d blog -c posts posts.json

Now use the aggregation framework to calculate the author with the greatest number of comments.

To help you verify your work before submitting, the author with the fewest comments is Mariela Sherer and she commented 387 times.

Please choose your answer below for the most prolific comment author:

Solution : 

Homework 5.1 m101p Mongodb for Developer,Anupam Singh pal, Shyam Softwares

Homework 5.2 : 

Crunching the Zipcode dataset
Please calculate the average population of cities in California (abbreviation CA) and New York (NY) (taken together) with populations over 25,000.
For this problem, assume that a city name that appears in more than one state represents two separate cities.
Please round the answer to a whole number.
Hint: The answer for CT and NJ (using this data set) is 38177.
Please note:
Different states might have the same city name.
A city might have multiple zip codes.

For this problem, we have used a subset of the data you previously used in zips.json, not the full set. For this set, there are only 200 documents (and 200 zip codes), and all of them are in New York, Connecticut, New Jersey, and California.
You can download the handout and perform your analysis on your machine with

mongoimport --drop -d test -c zips small_zips.json
Note

This is raw data from the United States Postal Service. If you notice that while importing, there are a few duplicates fear not, this is expected and will not affect your answer.

Once you've generated your aggregation query and found your answer, select it from the choices below.
Please use the Aggregation pipeline to solve this problem.
Solution : 44805

Homework 5.2 m101p Mongodb for Developer,Anupam Singh Pal, Shyam Software

Homework 5.3 :

Who's the easiest grader on campus?
Download the handout and mongoimport.
The documents look like this:

{
    "_id" : ObjectId("50b59cd75bed76f46522c392"),
    "student_id" : 10,
    "class_id" : 5,
    "scores" : [
        {
            "type" : "exam",
            "score" : 69.17634380939022
        },
        {
            "type" : "quiz",
            "score" : 61.20182926719762
        },
        {
            "type" : "homework",
            "score" : 73.3293624199466
        },
        {
            "type" : "homework",
            "score" : 15.206314042622903
        },
        {
            "type" : "homework",
            "score" : 36.75297723087603
        },
        {
            "type" : "homework",
            "score" : 64.42913107330241
        }
    ]
}
There are documents for each student (student_id) across a variety of classes (class_id). Note that not all students in the same class have the same exact number of assessments. Some students have three homework assignments, etc.

Your task is to calculate the class with the best average student performance. This involves calculating an average for each student in each class of all non-quiz assessments and then averaging those numbers to get a class average. To be clear, each student's average includes only exams and homework grades. Don't include their quiz scores in the calculation.

What is the class_id which has the highest average student performance?
Hint/Strategy: You need to group twice to solve this problem. You must figure out the GPA that each student has achieved in a class and then average those numbers to get a class average. After that, you just need to sort. The class with the lowest average is the class with class_id=2. Those students achieved a class average of 37.6
mongoimport --drop -d test -c grades grades.json
Below, choose the class_id with the highest average student average.

Solution : 1
Homework 5.3 m101p Mongodb for Developer,Anupam SIngh Pal,Shyam Softwares

Homework 5.4 :

Prefered Cities to Live!
In this problem you will calculate the number of people who live in a zip code in the US where the city starts with one of the following characthers:
  • BDOGN or M .
We will take these are the prefered cities to live in (chosen by this instructor, given is special affection to this set of characters!).
You will be using the zip code collection data set, which you will find in the 'handouts' link in this page.
Import it into your mongod using the following command from the command line:
mongoimport --drop -d test -c zips zips.json
If you imported it correctly, you can go to the test database in the mongo shell and confirm that
> db.zips.count()
yields 29353 documents.
The $project stage can extract the first character from any field. For example, to extract the first character from the city field, you could write this pipeline:
db.zips.aggregate([
    {$project:
     {
    first_char: {$substr : ["$city",0,1]},
     }
   }
])
Using the aggregation framework, calculate the sum total of people who are living in a zip code where the city starts with one of those possible first characters. Choose the answer below.
You will need to probably change your projection to send more info through than just that first character. Also, you will need a filtering step to get rid of all documents where the city does not start with the select set of initial characters.

                                                Solution :76394871

AGGREGATION FRAMEWORK, M101P, MongoDB for Developers,ShyamSoftwares



Try for your self 

db.zips.aggregate([ 
        { $project:{ _id : 0, city: 1, pop: 1          } },
        { $match:  { city: { $in: [ /^b.*/i, /^d.*/i, /^o.*/i, /^g.*/i, /^n.*/i, /^m.*/i ] } } },
        { $group:  { _id : null, pop: { $sum: "$pop" } } },
        { $sort:   { city: 1 } }
])


At the end I just want to say if you want to learn mongoDB, First practice with youself. because no one can steel talent 😇

No comments:

Post a Comment