MongoDB Exercise - Find restaurants in Manhattan or Brooklyn not in American or Chinese cuisine 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, and their cuisine is not American or Chinese.
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: [
{ borough: { $in: ["Manhattan", "Brooklyn"] } },
{ cuisine: { $nin: ["American", "Chinese"] } },
{ grades: { $elemMatch: { score: 2 } } },
{ grades: { $elemMatch: { score: 6 } } }
]
})
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 that searches for 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, and their cuisine is not American or Chinese.
The $and operator is used to combine all the conditions inside the array must be met.
The $in operator checks if the value of the borough field is either "Manhattan" or "Brooklyn".
The $nin operator checks if the value of the cuisine field is not "American" or "Chinese".
The $elemMatch operator checks if there is at least one element in the grades array where the score is equal to 2 and also the other element in the grades array where the score is equal to 6.
Note: This output is generated using MongoDB server version 3.6
Improve this sample solution and post your code through Disqus.
Previous: Find restaurants in Manhattan or Brooklyn not in American cuisine with grades of 2 and 6.
Next: Find restaurants with a grade of 2 or a grade of 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-42.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics