MongoDB Exercise - Find restaurants in Manhattan or Brooklyn with grades of 2 and 6
Write a MongoDB query to find the restaurants that have a grade with a score of 2 and a grade with a score of 6 and are located in the borough of Manhattan or Brooklyn.
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({
$and: [
{"grades.score": 2},
{"grades.score": 6},
{"borough": {"$in": ["Manhattan", "Brooklyn"]}}
]
})
Output:
{ "_id" : ObjectId("6422c7461238e3bec47ca63f"), "address" : { "building" : "261", "coord" : [ -73.94839189999999, 40.7224876 ], "street" : "Driggs Avenue", "zipcode" : "11222" }, "borough" : "Brooklyn", "cuisine" : "Polish", "grades" : [ { "date" : { "$date" : 1401494400000 }, "grade" : "A", "score" : 2 }, { "date" : { "$date" : 1368144000000 }, "grade" : "A", "score" : 3 }, { "date" : { "$date" : 1329436800000 }, "grade" : "A", "score" : 6 }, { "date" : { "$date" : 1318550400000 }, "grade" : "C", "score" : 54 } ], "name" : "Polish National Home", "restaurant_id" : "40364404" } { "_id" : ObjectId("6422c7dd1238e3bec47ca655"), "address" : { "building" : "567", "coord" : [ -74.00619499999999, 40.735663 ], "street" : "Hudson Street", "zipcode" : "10014" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : { "$date" : 1406505600000 }, "grade" : "A", "score" : 2 }, { "date" : { "$date" : 1374710400000 }, "grade" : "A", "score" : 7 }, { "date" : { "$date" : 1360022400000 }, "grade" : "A", "score" : 2 }, { "date" : { "$date" : 1338249600000 }, "grade" : "A", "score" : 6 }, { "date" : { "$date" : 1324598400000 }, "grade" : "A", "score" : 5 } ], "name" : "White Horse Tavern", "restaurant_id" : "40364958" } .....
Explanation:
The said query in MongoDB finds the restaurants that have a grade with a score of 2 and a grade with a score of 6 and are located in the borough of Manhattan or Brooklyn.
The $and operator checks if any of the grades array elements have a score field equal to 2 or 6, respectively and the $in operator to checks if the borough field matches either "Manhattan" or "Brooklyn".
Note: This output is generated using MongoDB server version 3.6
Improve this sample solution and post your code through Disqus.
Previous: The restaurants in Manhattan with grades of 2 and 6 are listed below.
Next: Find restaurants in Manhattan or Brooklyn not in American cuisine with grades of 2 and 6.
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://w3resource.com/mongodb-exercises/mongodb-exercise-40.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics