w3resource

MongoDB Exercise - Find the restaurant Id, name, borough and cuisine prepare specific dish except restaurant name begins with letter Wil


Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which prepared dish except 'American' and 'Chinees' or restaurant's name begins with letter 'Wil'.

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(
{$or: [
  {name: /^Wil/}, 
  {"$and": [
       {"cuisine" : {$ne :"American "}}, 
       {"cuisine" : {$ne :"Chinees"}}
   ]}
]}
,{"restaurant_id" : 1,"name":1,"borough":1,"cuisine" :1}
);

Output:

{ "_id" : ObjectId("564c2d939eb21ad392f175c9"), "borough" : "Manhattan", "cuisine" : "Irish", "name" : "Dj Reynolds Pub And Restaurant", "restaurant_id" : "30191841" }
{ "_id" : ObjectId("564c2d939eb21ad392f175ca"), "borough" : "Bronx", "cuisine" : "Bakery", "name" : "Morris Park Bake Shop", "restaurant_id" : "30075445" }
{ "_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("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("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("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("564c2d939eb21ad392f175de"), "borough" : "Manhattan", "cuisine" : "Delicatessen", "name" : "Bully'S Deli", "restaurant_id" : "40361708" }
{ "_id" : ObjectId("564c2d939eb21ad392f175df"), "borough" : "Queens", "cuisine" : "Delicatessen", "name" : "Steve Chu'S Deli & Grocery", "restaurant_id" : "40361998" }
{ "_id" : ObjectId("564c2d939eb21ad392f175e0"), "borough" : "Manhattan", "cuisine" : "Chicken", "name" : "Harriet'S Kitchen", "restaurant_id" : "40362098" }
{ "_id" : ObjectId("564c2d939eb21ad392f175e3"), "borough" : "Manhattan", "cuisine" : "Turkish", "name" : "The Country Cafe", "restaurant_id" : "40362715" }
{ "_id" : ObjectId("564c2d939eb21ad392f175e4"), "borough" : "Queens", "cuisine" : "Chinese", "name" : "Ho Mei Restaurant", "restaurant_id" : "40362432" }
Type "it" for more

Note: This output is generated using MongoDB server version 3.6

Explanation:

The said query in MongoDB that searches for restaurants whose name starts with "Wil" or whose cuisine is not "American " or "Chinees". The result documents will only include the "restaurant_id", "name", "borough", and "cuisine" fields.
The { $or: [...] } method matches any document that satisfies at least one of the conditions within two or more values.
The { name: /^Wil/ } method matches documents where the "name" field starts with "Wil".
The { "$and": [ { ... }, { ... } ] } method matches documents where both conditions specified inside the array are satisfied.
The { cuisine: { $ne: "American " } } matches documents where the "cuisine" field is not equal to "American " and the { cuisine: { $ne: "Chinees" } } matches documents where the "cuisine" field is not equal to "Chinees".

Improve this sample solution and post your code through Disqus.

Previous: Find the restaurant ID, name, borough and cuisine for those restaurants with a score not exceeding 10.
Next: Find restaurant IDs, names, and grades on ISODate "2014-08-11T00:00:00Z" among many of the survey dates for restaurants that scored an "A".

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.