w3resource

MongoDB Exercise - Find the number of 'A'-rated restaurants by cuisine and borough


Write a MongoDB query to find the count of restaurants that received a grade of 'A' for each cuisine and borough.

Structure of 'restaurants' collection :

{
  "address": {
     "building": "1007",
     "coord": [ -73.856077, 40.848447 ],
     "street": "Morris Park Ave",
     "zipcode": "10462"
  },
  "borough": "Bronx",
  "cuisine": "Bakery",
  "grades": [
     { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
     { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
     { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
     { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
     { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
  ],
  "name": "Morris Park Bake Shop",
  "restaurant_id": "30075445"
}

Query:

db.restaurants.aggregate([
  {
    $match: { "grades.grade": "A" }
  },
  {
    $group: {
      _id: { cuisine: "$cuisine", borough: "$borough" },
count: { $sum: 1 }
    }
  },
  {
    $sort: { count: -1 }
  }
]);

Output:

{ _id: { cuisine: 'American ', borough: 'Manhattan' }, count: 731 },
{ _id: { cuisine: 'Italian', borough: 'Manhattan' }, count: 204 },
{ _id: { cuisine: 'American ', borough: 'Queens' }, count: 192 },
{ _id: { cuisine: 'American ', borough: 'Brooklyn' }, count: 190 },
  {
    _id: { cuisine: 'Café/Coffee/Tea', borough: 'Manhattan' },
count: 133
  },
{ _id: { cuisine: 'Pizza', borough: 'Queens' }, count: 80 },
{ _id: { cuisine: 'American ', borough: 'Bronx' }, count: 74 },
{ _id: { cuisine: 'Pizza', borough: 'Manhattan' }, count: 73 },
{ _id: { cuisine: 'Pizza', borough: 'Brooklyn' }, count: 72 },
{ _id: { cuisine: 'French', borough: 'Manhattan' }, count: 68 },
{ _id: { cuisine: 'Japanese', borough: 'Manhattan' }, count: 68 },
.....

Explanation:

The said query in MongoDB that returns a result set that includes each cuisine-borough pair and the count of restaurants that received a grade of 'A' for that pair.

The $match stage filters the restaurants that received a grade of 'A'. The dot notation have used to access the nested grades array.

The $group stage groups the restaurants by cuisine and borough, and calculates the count of restaurants using the $sum operator.

$sort stage sorts the result by the count in descending order, so that the cuisine-borough pairs with the highest counts appear first.

Note: This output is generated using MongoDB server version 3.6

Improve this sample solution and post your code through Disqus.

Previous: For each borough, find the number of restaurants with an 'A'.
Next: Find out how many restaurants have been graded in each month.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb-exercises/mongodb-exercise-59.php