w3resource

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

Improve this sample solution and post your code through Disqus.

Previous: Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which prepared dish except 'American' and 'Chinees' or restaurant's name begins with letter 'Wil'.
Next: Write a MongoDB query to find the restaurant Id, name, and grades for those restaurants where the 2nd element of grades array contains a grade of "A" and score 9 on an ISODate "2014-08-11T00:00:00Z".

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming