Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

MongoDB Exercise - Find the restaurant Id, name, borough and cuisine for those restaurants which contain Wil as first three letters for its name


Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which contain 'Wil' as first three letters for its name.

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(
{name: /^Wil/},
{
"restaurant_id" : 1,
"name":1,"borough":1,
"cuisine" :1
}
);

Output:

{ "_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("564c2d949eb21ad392f183d7"), "borough" : "Bronx", "cuisine" : "Pizza", "name" : "Wilbel Pizza", "restaurant_id" : "40871979" }
{ "_id" : ObjectId("564c2d949eb21ad392f18528"), "borough" : "Manhattan", "cuisine" : "Seafood", "name" : "Wild Edibles", "restaurant_id" : "40928482" }
{ "_id" : ObjectId("564c2d949eb21ad392f19063"), "borough" : "Brooklyn", "cuisine" : "Bagels/Pretzels", "name" : "Wild Bagels", "restaurant_id" : "41225826" }
{ "_id" : ObjectId("564c2d949eb21ad392f19140"), "borough" : "Bronx", "cuisine" : "Latin (Cuban, Dominican, Puerto Rican, South & Central American)", "name" : "Willie'S Steak House", "restaurant_id" : "41239497" }
{ "_id" : ObjectId("564c2d949eb21ad392f19238"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "Willis North America", "restaurant_id" : "41255267" }
{ "_id" : ObjectId("564c2d949eb21ad392f19239"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "Willburg Cafe", "restaurant_id" : "41255152" }
{ "_id" : ObjectId("564c2d949eb21ad392f1980a"), "borough" : "Manhattan", "cuisine" : "Café/Coffee/Tea", "name" : "William Greenberg Jr Desserts", "restaurant_id" : "41353639" }
{ "_id" : ObjectId("564c2d949eb21ad392f198fa"), "borough" : "Brooklyn", "cuisine" : "Vegetarian", "name" : "Wild Ginger Vegan Cafe", "restaurant_id" : "41369777" }
{ "_id" : ObjectId("564c2d949eb21ad392f19930"), "borough" : "Manhattan", "cuisine" : "Irish", "name" : "Wilfie & Nell", "restaurant_id" : "41372275" }
{ "_id" : ObjectId("564c2d949eb21ad392f19e43"), "borough" : "Manhattan", "cuisine" : "Vegetarian", "name" : "Wild Ginger Vegetarian Kitchen", "restaurant_id" : "41433964" }
{ "_id" : ObjectId("564c2d949eb21ad392f19f32"), "borough" : "Manhattan", "cuisine" : "Pizza", "name" : "Wild", "restaurant_id" : "41447856" }
{ "_id" : ObjectId("564c2d949eb21ad392f1ab93"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "William Barnacle", "restaurant_id" : "41591881" }
{ "_id" : ObjectId("564c2d949eb21ad392f1ac17"), "borough" : "Queens", "cuisine" : "American ", "name" : "William Hallet", "restaurant_id" : "41597549" }
{ "_id" : ObjectId("564c2d949eb21ad392f1ac36"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "Williams & Bailey", "restaurant_id" : "41598951" }
{ "_id" : ObjectId("564c2d949eb21ad392f1ac66"), "borough" : "Manhattan", "cuisine" : "Asian", "name" : "Wild Ginger", "restaurant_id" : "41600577" }
{ "_id" : ObjectId("564c2d949eb21ad392f1b41f"), "borough" : "Manhattan", "cuisine" : "Bakery", "name" : "William Greenberg Dessert", "restaurant_id" : "41657368" }
{ "_id" : ObjectId("564c2d949eb21ad392f1b5af"), "borough" : "Brooklyn", "cuisine" : "Pizza/Italian", "name" : "Williamsburg Pizza", "restaurant_id" : "41672156" }
{ "_id" : ObjectId("564c2d949eb21ad392f1b609"), "borough" : "Queens", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Wild Cherry", "restaurant_id" : "41675246" }
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.

Previous: Write a MongoDB query to find the restaurants which do not prepare any cuisine of 'American ' and achieved a grade point 'A' not belongs to the borough Brooklyn.The document must be displayed according to the cuisine in descending order.
Next: Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which contain 'ces' as last three letters for its name.

What is the difficulty level of this exercise?