MongoDB Exercise - Find the restaurant Id, name, borough and cuisine for those restaurants which belong to the borough Staten Island or Queens or Bronxor Brooklyn
Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which belong to the borough Staten Island or Queens or Bronxor 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(
{"borough" :{$in :["Staten Island","Queens","Bronx","Brooklyn"]}},
{
"restaurant_id" : 1,
"name":1,"borough":1,
"cuisine" :1
}
);
Output:
{ "_id" : ObjectId("564c2d939eb21ad392f175ca"), "borough" : "Bronx", "cuisine" : "Bakery", "name" : "Morris Park Bake Shop", "restaurant_id" : "30075445" } { "_id" : ObjectId("564c2d939eb21ad392f175cb"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "Riviera Caterer", "restaurant_id" : "40356018" } { "_id" : ObjectId("564c2d939eb21ad392f175cc"), "borough" : "Brooklyn", "cuisine" : "Hamburgers", "name" : "Wendy'S", "restaurant_id" : "30112340" } { "_id" : ObjectId("564c2d939eb21ad392f175cd"), "borough" : "Queens", "cuisine" : "Jewish/Kosher", "name" : "Tov Kosher Kitchen", "restaurant_id" : "40356068" } { "_id" : ObjectId("564c2d939eb21ad392f175ce"), "borough" : "Queens", "cuisine" : "American ", "name" : "Brunos On The Boulevard", "restaurant_id" : "40356151" } { "_id" : ObjectId("564c2d939eb21ad392f175cf"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "Regina Caterers", "restaurant_id" : "40356649" } { "_id" : ObjectId("564c2d939eb21ad392f175d0"), "borough" : "Brooklyn", "cuisine" : "Delicatessen", "name" : "Wilken'S Fine Food", "restaurant_id" : "40356483" } { "_id" : ObjectId("564c2d939eb21ad392f175d1"), "borough" : "Bronx", "cuisine" : "American ", "name" : "Wild Asia", "restaurant_id" : "40357217" } { "_id" : ObjectId("564c2d939eb21ad392f175d2"), "borough" : "Brooklyn", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Taste The Tropics Ice Cream", "restaurant_id" : "40356731" } { "_id" : ObjectId("564c2d939eb21ad392f175d3"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "C & C Catering Service", "restaurant_id" : "40357437" } { "_id" : ObjectId("564c2d939eb21ad392f175d4"), "borough" : "Brooklyn", "cuisine" : "Chinese", "name" : "May May Kitchen", "restaurant_id" : "40358429" } { "_id" : ObjectId("564c2d939eb21ad392f175d6"), "borough" : "Brooklyn", "cuisine" : "Jewish/Kosher", "name" : "Seuda Foods", "restaurant_id" : "40360045" } { "_id" : ObjectId("564c2d949eb21ad392f1c593"), "borough" : "Queens", "cuisine" : "Other", "name" : "Laquana King", "restaurant_id" : "50003441" } { "_id" : ObjectId("564c2d939eb21ad392f175d7"), "borough" : "Brooklyn", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Carvel Ice Cream", "restaurant_id" : "40360076" } { "_id" : ObjectId("564c2d939eb21ad392f175d8"), "borough" : "Queens", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Carvel Ice Cream", "restaurant_id" : "40361322" } { "_id" : ObjectId("564c2d939eb21ad392f175d9"), "borough" : "Brooklyn", "cuisine" : "Delicatessen", "name" : "Nordic Delicacies", "restaurant_id" : "40361390" } { "_id" : ObjectId("564c2d939eb21ad392f175da"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "The Movable Feast", "restaurant_id" : "40361606" } { "_id" : ObjectId("564c2d939eb21ad392f175dc"), "borough" : "Queens", "cuisine" : "Delicatessen", "name" : "Sal'S Deli", "restaurant_id" : "40361618" } { "_id" : ObjectId("564c2d939eb21ad392f175dd"), "borough" : "Staten Island", "cuisine" : "Jewish/Kosher", "name" : "Kosher Island", "restaurant_id" : "40356442" } { "_id" : ObjectId("564c2d939eb21ad392f175df"), "borough" : "Queens", "cuisine" : "Delicatessen", "name" : "Steve Chu'S Deli & Grocery", "restaurant_id" : "40361998" } Type "it" for more
Note: This output is generated using MongoDB server version 3.6
Explanation:
The said query in MongoDB that is searching for restaurants in four specific boroughs of New York City, and returning "restaurant_id", "name", "borough", and "cuisine" fields about those restaurants in the 'restaurants' collection.
The query uses the MongoDB "$in" operator to specify that a document's "borough" field must be equal to at least one of the four specified values in the array ["Staten Island","Queens","Bronx","Brooklyn"] in order to be returned in the query results.
Improve this sample solution and post your code through Disqus.
Previous: Find restaurants in the Bronx that prepare American or Chinese food.
Next: Identify restaurants not in Staten Island, Queens, Bronx, or Brooklyn by their restaurant id, name, and borough.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics