Exercises

Exercise 1

  • Use restaurants.json

  • Using aggregation framework, print documents below.

    1. Find Korean cuisine

    2. Group by borough and count the cuisines.

    [{'_id': 'Brooklyn', 'count': 16},
    {'_id': 'Queens', 'count': 167},
    {'_id': 'Staten Island', 'count': 1},
    {'_id': 'Manhattan', 'count': 78}]

Exercise 2

  • Using aggregation framework, print documents below.

    1. Find Korean cuisine

    2. Unwind grades

    3. Group by borough and grade in grades, and count the document

    4. Sort by count and print five documents

    [{'_id': {'borough': 'Queens', 'grade': 'A'}, 'count': 443},
    {'_id': {'borough': 'Manhattan', 'grade': 'A'}, 'count': 232},
    {'_id': {'borough': 'Queens', 'grade': 'B'}, 'count': 140},
    {'_id': {'borough': 'Manhattan', 'grade': 'B'}, 'count': 49},
    {'_id': {'borough': 'Brooklyn', 'grade': 'A'}, 'count': 38}]

Exercise 3

  • Find international airports in California using aggregate framework

  1. states collection

    • Get the California location (Polygon)

  2. airports collection

    • Find “intl” in name field (text)

    • Find airports in the California

    • Project name, type and code (w/o _id)

    • Sort by name (ascending order) and code (descending order)

Exercise 4

  • Find Korean restaurants that are located at less 2km from international airports (use the type field).

    • Warn: should create 2dsphere index to address for restaurants collection.

Exercise 5

  • Print the average, minimum, and maximum of passengers(ride_pasgr_numfield) of each subway line.

  • Hint: group by the ‘line_num’ field and use $max, $min, and $avg.

  • Result

Exercise 6

  • Print the total number of passengers (ride_pasgr_numfield) during 2017-12-21 ~ 2017-12-28.

  • ‘use_dt’ is a stringtype.

  • Hint: use $match,$gt$lt, $sum, $sort

  • Result

Last updated

Was this helpful?