w3resource

MongoDB Exercise - Find out how many restaurants have been graded in each month


Write a MongoDB query to find the number of restaurants that have been graded in each month of the year.

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([
  {
    $unwind: "$grades"
  },
  {
    $project: {
month: { $month: { $toDate: "$grades.date" } },
year: { $year: { $toDate: "$grades.date" } }
    }
  },
  {
    $group: {
      _id: { month: "$month", year: "$year" },
count: { $sum: 1 }
    }
  },
  {
    $sort: {
      "_id.year": 1,
      "_id.month": 1
    }
  }
]);

Output:

{ _id: { month: 12, year: 2010 }, count: 3 },
{ _id: { month: 1, year: 2011 }, count: 5 },
{ _id: { month: 2, year: 2011 }, count: 20 },
{ _id: { month: 3, year: 2011 }, count: 65 },
{ _id: { month: 4, year: 2011 }, count: 66 },
{ _id: { month: 5, year: 2011 }, count: 132 },
{ _id: { month: 6, year: 2011 }, count: 154 },
{ _id: { month: 7, year: 2011 }, count: 144 },
{ _id: { month: 8, year: 2011 }, count: 179 },
{ _id: { month: 9, year: 2011 }, count: 263 },
{ _id: { month: 10, year: 2011 }, count: 328 },
{ _id: { month: 11, year: 2011 }, count: 378 },
{ _id: { month: 12, year: 2011 }, count: 513 },
{ _id: { month: 1, year: 2012 }, count: 512 },
{ _id: { month: 2, year: 2012 }, count: 502 },
{ _id: { month: 3, year: 2012 }, count: 488 },
{ _id: { month: 4, year: 2012 }, count: 501 },
{ _id: { month: 5, year: 2012 }, count: 551 },
{ _id: { month: 6, year: 2012 }, count: 428 },
{ _id: { month: 7, year: 2012 }, count: 354 }
.....

Explanation:

The said query in MongoDB that returns a list of documents, where each document contains the month and year, as well as the count of restaurants that have been graded in that month and year.

The $unwind stage in aggregation pipeline, which is used to create a new document for each element in the grades array field.

The $project in aggregation pipeline is used to create new fields based on existing fields in the documents.

The $toDate operator converts the date field in the grades subdocument to a date object, and then use the $month and $year operators extracts the month and year from the date object.

The $group groups the documents based on the month and year field. The $sum operator counts the number of documents in each group and the $sort stage is used to sort the results by year and month.

Note: This output is generated using MongoDB server version 3.6

Improve this sample solution and post your code through Disqus.

Previous: Find the number of 'A'-rated restaurants by cuisine and borough.
Next: Find the average score for each cuisine.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.