MongoDB Exercise - Find restaurants in Manhattan or Brooklyn not in American 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.
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"]}},
{"grades.score": {$all: [2, 6]}},
{cuisine: {$ne: "American"}}
]
})
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 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, and their cuisine is not American:
Within the $and operator the $in operator specified that the borough must be either "Manhattan" or "Brooklyn" and the $all operator specified that the grades must contain at least two sub-documents with a score that matches 2 and 6 respectively.
The $ne operator specified that the cuisine must not be "American".
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 with grades of 2 and 6.
Next: Find restaurants in Manhattan or Brooklyn not in American or Chinese 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-41.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics