MongoDB Exercise - The restaurants in Manhattan with grades of 2 and 6 are listed below
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.
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": "Manhattan"}
]
})
Output:
{ "_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" } { "_id" : ObjectId("6422cadd1238e3bec47ca661"), "address" : { "building" : "842", "coord" : [ -73.97063700000001, 40.751495 ], "street" : "2 Avenue", "zipcode" : "10017" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : { "$date" : 1405987200000 }, "grade" : "A", "score" : 6 }, { "date" : { "$date" : 1369699200000 }, "grade" : "A", "score" : 2 }, { "date" : { "$date" : 1338249600000 }, "grade" : "A", "score" : 8 }, { "date" : { "$date" : 1325721600000 }, "grade" : "A", "score" : 9 }, { "date" : { "$date" : 1312934400000 }, "grade" : "B", "score" : 24 } ], "name" : "Keats Restaurant", "restaurant_id" : "40365288" } .....
Explanation:
The said query in MongoDB that returns 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.
The uses of $and operator combines the first two conditions that check whether any of the grades array elements have a score field equal to 2 or 6, respectively and the third condition checks if the borough field is equal to "Manhattan".
Note: This output is generated using MongoDB server version 3.6
Improve this sample solution and post your code through Disqus.
Previous: Find the restaurants with a grade of 2 and a grade of 6.
Next: Find restaurants in Manhattan or Brooklyn 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-39.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics