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("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("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("564c2d949eb21ad392f1aae6"), "grades" : [ { "date" : ISODate("2015-01-06T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-08-11T00:00:00Z"), "grade" : "A", "score" : 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
Explanation:
The said query in MongoDB searches for restaurants that must have a grade of "A" on its second inspection on August 11th, 2014, and it must have received a score of 9 on its second inspection.
The query will return the restaurant_id, name, and grades fields for each matching restaurant.
The condition searches for a specific date for the second inspection, specified as an ISODate object "grades.1.date": ISODate("2014-08-11T00:00:00Z").
The "grades.1.grade":"A" searches for a grade of "A" on the second inspection and the "grades.1.score" : 9 searches for a score of 9 on the second inspection.
A value of 1 means that the field should be included, while a value of 0 would exclude the field.
Improve this sample solution and post your code through Disqus.
Previous: Find restaurant IDs, names, and grades on ISODate "2014-08-11T00:00:00Z" among many of the survey dates for restaurants that scored an "A".
Next: Restaurant Id, name, address, and geographical location for those restaurants whose coord array has a value higher than 42 or up to 52.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics