MongoDB Exercise - Find the restaurant Id, name, and grades for those restaurants which achieved a specific grade, score on a specific date
Write a MongoDB query to find the restaurant Id, name, and grades for those restaurants which achieved a grade of "A" and scored 11 on an ISODate "2014-08-11T00:00:00Z" among many of survey dates.
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.find(
{
"grades.date": ISODate("2014-08-11T00:00:00Z"),
"grades.grade":"A" ,
"grades.score" : 11
},
{"restaurant_id" : 1,"name":1,"grades":1}
);
Output:
{ "_id" : ObjectId("564c2d939eb21ad392f17648"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-07-22T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-03-14T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-07-02T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-02-02T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-08-24T00:00:00Z"), "grade" : "A", "score " : 11 } ], "name" : "Neary'S Pub", "restaurant_id" : "40365871" } { "_id" : ObjectId("564c2d939eb21ad392f17721"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2013-12-10T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-06-10T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-06-08T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-01-25T00:00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2011-09-13T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Don Filippo Restaurant", "restaurant_id" : "40372417" } { "_id" : ObjectId("564c2d949eb21ad392f179e7"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 6 }, { "date" : ISODate("2013-08-29T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-04-25T00:00:00Z"), "grade" : "C", "score" : 2 }, { "date" : ISODate("2012-10-23T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-04-16T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-11-15T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Mustang Sally'S Restaurant", "restaurant_id" : "40397374" } { "_id" : ObjectId("564c2d949eb21ad392f17bf3"), "grades" : [ { "date" : ISODate("2015-01-12T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2014-01-14T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-02-07T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2012-04-30T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Club Macanudo (Cigar Bar)", "restaurant_id" : "405264 06" } { "_id" : ObjectId("564c2d949eb21ad392f17ce9"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2014-01-09T00:00:00Z"), "grade" : "B", "score" : 27 }, { "date" : ISODate("2013-06-11T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2012-10-09T00:00:00Z"), "grade" : "B", "score" : 19 }, { "date" : ISODate("2012-05-10T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-11-23T00:00:00Z"), "grade" : "A", "score" : 9 } ], "name" : "Marino'S Pizza & Restaurant", "restaurant_id" : "40560917" } { "_id" : ObjectId("564c2d949eb21ad392f17e98"), "grades" : [ { "date" : ISODate("2015-01-05T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-03-04T00:00:00Z"), "grade" : "B", "score" : 22 }, { "date" : ISODate("2013-09-13T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2013-01-16T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Gene'S Coffee Shop", "restaurant_id" : "40614916" } { "_id" : ObjectId("564c2d949eb21ad392f17fff"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-02-20T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2013-08-29T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2013-03-29T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2012-03-07T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2011-11-18T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-10-12T00:00:00Z"), "grade" : "P", "score" : 4 } ], "name" : "Union Cafe Restaurant", "restaurant_id" : "40698823" } { "_id" : ObjectId("564c2d949eb21ad392f1846b"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-08-28T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-09-05T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-08-25T00:00:00Z"), "grade" : "A", "score" : 7 } ], "name" : "Jojo'S Pizza", "restaurant_id" : "40892913" } { "_id" : ObjectId("564c2d949eb21ad392f184b9"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-08-29T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2013-04-01T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-10-23T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2011-09-30T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Cosi Sandwich Bar", "restaurant_id" : "40912833" } { "_id" : ObjectId("564c2d949eb21ad392f184f9"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-02-04T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-06-19T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2012-06-25T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-02-08T00:00:00Z"), "grade" : "A", "score" : 5 } ], "name" : "Cosi", "restaurant_id" : "40922983" } { "_id" : ObjectId("564c2d949eb21ad392f1851c"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-02-13T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-06-04T00:00:00Z"), "grade" : "B", "score" : 15 }, { "date" : ISODate("2012-12-12T00:00:00Z"), "grade" : "B", "score" : 17 }, { "date" : ISODate("2012-05-17T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Isla Of Verde", "restaurant_id" : "40927513" } { "_id" : ObjectId("564c2d949eb21ad392f18699"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-08-19T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-08-23T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-02-10T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2011-10-20T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Bamboo Garden Chinese Restaurant", "restaurant_id" : " 40970116" } { "_id" : ObjectId("564c2d949eb21ad392f189e8"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-03-07T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2013-08-27T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-03-28T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-10-05T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-05-07T00:00:00Z"), "grade" : "A", "score " : 12 }, { "date" : ISODate("2011-12-08T00:00:00Z"), "grade" : "A", "score" : 9 } ], "name" : "Tina'S Cuban Cuisine", "restaurant_id" : "41067289" } { "_id" : ObjectId("564c2d949eb21ad392f18b3d"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2014-01-27T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-01-16T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-05-15T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2011-12-22T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2011-08-18T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Bar Americain", "restaurant_id" : "41101050" } { "_id" : ObjectId("564c2d949eb21ad392f190a5"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-02-07T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-12-05T00:00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2012-07-13T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-02-08T00:00:00Z"), "grade" : "A", "score" : 9 } ], "name" : "Subway", "restaurant_id" : "41230741" } { "_id" : ObjectId("564c2d949eb21ad392f19103"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2014-01-29T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-07-22T00:00:00Z"), "grade" : "C", "score" : 37 }, { "date" : ISODate("2013-01-11T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-05-15T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Pizza & Pita", "restaurant_id" : "41236336" } { "_id" : ObjectId("564c2d949eb21ad392f194e1"), "grades" : [ { "date" : ISODate("2015-01-15T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-06-18T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2013-01-24T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2012-06-11T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Shoolbred'S", "restaurant_id" : "41302014" } { "_id" : ObjectId("564c2d949eb21ad392f19580"), "grades" : [ { "date" : ISODate("2014-12-27T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2014-02-24T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-01-08T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-06-27T00:00:00Z"), "grade" : "A", "score" : 6 }, { "date" : ISODate("2012-01-26T00:00:00Z"), "grade" : "A", "score" : 5 } ], "name" : "Cabana", "restaurant_id" : "41309951" } { "_id" : ObjectId("564c2d949eb21ad392f196c2"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2014-01-27T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-07-31T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-07-31T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-12-19T00:00:00Z"), "grade" : "B", "score" : 19 } ], "name" : "Mike'S Island Grill Restaurant", "restaurant_id" : "4 1330799" } { "_id" : ObjectId("564c2d949eb21ad392f198b7"), "grades" : [ { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "B", "score" : 17 }, { "date" : ISODate("2014-01-28T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-04-04T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-10-04T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-02-23T00:00:00Z"), "grade" : "A", "score" : 9 } ], "name" : "Mansions Catering", "restaurant_id" : "41365614" } Type "it" for more
Note: This output is generated using MongoDB server version 3.6
Explanation:
The said query in MongoDB that searches for restaurants that received an "A" grade with a score of 11 on August 11th, 2014. The result documents will only include the "restaurant_id", "name", and "grades" fields.
The search criteria will match documents where the "grades" field contains an array of subdocuments, where at least one subdocument has a "date" field that matches the ISODate "2014-08-11T00:00:00Z".
The same subdocument also has a "grade" field that equals "A" and also has a "score" field that equals 11.
Improve this sample solution and post your code through Disqus.
Previous: Find the Id, name, borough, and cuisine of restaurants that don't prepare American or Chinese dishes or whose names begin with Wil.
Next: Id, name, and grades of restaurants with the 2nd element of grades array containing "A" and score 9 on ISODate "2014-08-11T00:00:00Z".
What is the difficulty level of this exercise?
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://www.w3resource.com/mongodb-exercises/mongodb-exercise-22.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics