w3resource
w3resource logo

MongoDB Exercise - Find the restaurant Id, name, borough and cuisine for those restaurants which are not belonging 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 are not belonging 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" :{$nin :["Staten Island","Queens","Bronx","Brooklyn"]}},
{
"restaurant_id" : 1,
"name":1,"borough":1,
"cuisine" :1
}
);

Output:

{ "_id" : ObjectId("5ab23ff163b071e80805c4c6"), "borough" : "Manhattan", "cuisine" : "Irish", "name" : "Dj Reynolds Pub And Restaurant", "restaurant_id" : "30191841" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4d1"), "borough" : "Manhattan", "cuisine" : "American", "name" : "1 East 66Th Street Kitchen", "restaurant_id" : "40359480" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4d6"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Glorious Food", "restaurant_id" : "40361521" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4d8"), "borough" : "Manhattan", "cuisine" : "Delicatessen", "name" : "Bully'S Deli", "restaurant_id" : "40361708" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4db"), "borough" : "Manhattan", "cuisine" : "Chicken", "name" : "Harriet'S Kitchen", "restaurant_id" : "40362098" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4dc"), "borough" : "Manhattan", "cuisine" : "American", "name" : "P & S Deli Grocery", "restaurant_id" : "40362264" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4de"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Angelika Film Center", "restaurant_id" : "40362274" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4df"), "borough" : "Manhattan", "cuisine" : "Turkish", "name" : "The Country Cafe", "restaurant_id" : "40362715" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4e3"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Downtown Deli", "restaurant_id" : "40363021" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4e7"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Cafe Metro", "restaurant_id" : "40363298" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4e9"), "borough" : "Manhattan", "cuisine" : "Bakery", "name" : "Olive'S", "restaurant_id" : "40363151" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4ea"), "borough" : "Manhattan", "cuisine" : "Sandwiches/Salads/Mixed Buffet", "name" : "Lexler Deli", "restaurant_id" : "40363426" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4ee"), "borough" : "Manhattan", "cuisine" : "Continental", "name" : "Lorenzo & Maria'S", "restaurant_id" : "40363630" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4ef"), "borough" : "Manhattan", "cuisine" : "Pizza", "name" : "Domino'S Pizza", "restaurant_id" : "40363644" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4f1"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Berkely", "restaurant_id" : "40363685" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4f4"), "borough" : "Manhattan", "cuisine" : "Pizza", "name" : "Domino'S Pizza", "restaurant_id" : "40363945" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4f6"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Spoon Bread Catering", "restaurant_id" : "40364179" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4fb"), "borough" : "Manhattan", "cuisine" : "Chicken", "name" : "Texas Rotisserie", "restaurant_id" : "40364304" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4fe"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Metropolitan Club", "restaurant_id" : "40364347" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4ff"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Palm Restaurant", "restaurant_id" : "40364355" }
Type "it" for more

Note: This output is generated using MongoDB server version 3.6

Improve this sample solution and post your code through Disqus.



Amazon promo codes to get huge discounts for limited period (USA only).