MongoDB Exercise - Find restaurants with grades greater than 5 and located in the borough Manhattan
Write a MongoDB query to find the restaurants that have all grades with a score greater than 5 and are located in the borough of Manhattan.
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({
"borough": "Manhattan",
"grades": {
"$not": {
"$elemMatch": {
"score": {
"$lte": 5
}
}
}
}
})
Output:
{ "_id" : ObjectId("6422c02d1238e3bec47ca611"), "address" : { "building" : "522", "coord" : [ -73.95171, 40.767461 ], "street" : "East 74 Street", "zipcode" : "10021" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : { "$date" : 1409616000000 }, "grade" : "A", "score" : 12 }, { "date" : { "$date" : 1387411200000 }, "grade" : "B", "score" : 16 }, { "date" : { "$date" : 1369699200000 }, "grade" : "A", "score" : 9 }, { "date" : { "$date" : 1354838400000 }, "grade" : "A", "score" : 13 }, { "date" : { "$date" : 1332979200000 }, "grade" : "A", "score" : 11 } ], "name" : "Glorious Food", "restaurant_id" : "40361521" } { "_id" : ObjectId("6422c04d1238e3bec47ca614"), "address" : { "building" : "759", "coord" : [ -73.9925306, 40.7309346 ], "street" : "Broadway", "zipcode" : "10003" }, "borough" : "Manhattan", "cuisine" : "Delicatessen", "grades" : [ { "date" : { "$date" : 1390262400000 }, "grade" : "A", "score" : 12 }, { "date" : { "$date" : 1357257600000 }, "grade" : "A", "score" : 11 }, { "date" : { "$date" : 1339027200000 }, "grade" : "A", "score" : 6 }, { "date" : { "$date" : 1326758400000 }, "grade" : "A", "score" : 8 } ], "name" : "Bully'S Deli", "restaurant_id" : "40361708" } .....
Explanation:
The said query in MongoDB that returns all documents in the 'restaurants' collection that have at least one grade with a score of greater than 5 and that are located in the borough of Manhattan.
The condition searches for documents where the "grades" field contains at least one sub-document where the "score" field has a value more than 5, and the "borough" field has a value of "Manhattan".
Note: This output is generated using MongoDB server version 3.6
Improve this sample solution and post your code through Disqus.
Previous: All restaurants with a score of 5 or higher.
Next: Find restaurants in Manhattan or Brooklyn that have at least one grade above 5.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics