w3resource
w3resource logo

MongoDB Exercise - Find the restaurant Id, name, and grades where the 2nd element of grades array contains a grade of A and score 9 on a specific date


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".

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.1.date": ISODate("2014-08-11T00:00:00Z"), 
                        "grades.1.grade":"A" , 
                        "grades.1.score" : 9
                      }, 
                       {"restaurant_id" : 1,"name":1,"grades":1}
                   );

Output:

{ "_id" : ObjectId("5ab23ff163b071e80805caf0"), "grades" : [ { "date" : ISODate("2015-01-12T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "scor
e" : 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" : "40526406" }
{ "_id" : ObjectId("5ab23ff163b071e80805e3e4"), "grades" : [ { "date" : ISODate("2015-01-15T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "scor
e" : 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("5ab23ff163b071e80805e483"), "grades" : [ { "date" : ISODate("2014-12-27T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "scor
e" : 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("5ab23ff163b071e80805f9ed"), "grades" : [ { "date" : ISODate("2015-01-06T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "scor
e" : 9 }, { "date" : ISODate("2014-01-30T00:00:00Z"), "grade" : "B", "score" : 15 } ], "name" : "Obao Noodles & Bbq", "restaurant_id" : "41585019" }
>

Note: This output is generated using MongoDB server version 3.6

Improve this sample solution and post your code through Disqus.