w3resource
w3resource logo

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("5ab23ff163b071e80805c4ca"), "borough" : "Brooklyn", "cuisine" : "Delicatessen", "name" : "Wilken'S Fine Food", "restaurant_id" : "40356483" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4ce"), "borough" : "Bronx", "cuisine" : "American", "name" : "Wild Asia", "restaurant_id" : "40357217" }
{ "_id" : ObjectId("5ab23ff163b071e80805d2d7"), "borough" : "Bronx", "cuisine" : "Pizza", "name" : "Wilbel Pizza", "restaurant_id" : "40871979" }
{ "_id" : ObjectId("5ab23ff163b071e80805d427"), "borough" : "Manhattan", "cuisine" : "Seafood", "name" : "Wild Edibles", "restaurant_id" : "40928482" }
{ "_id" : ObjectId("5ab23ff163b071e80805df65"), "borough" : "Brooklyn", "cuisine" : "Bagels/Pretzels", "name" : "Wild Bagels", "restaurant_id" : "41225826" }
{ "_id" : ObjectId("5ab23ff163b071e80805e043"), "borough" : "Bronx", "cuisine" : "Latin (Cuban, Dominican, Puerto Rican, South & Central American)", "name" : "Willie'S Steak House", "restaurant_id" :
"41239497" }
{ "_id" : ObjectId("5ab23ff163b071e80805e13a"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Willis North America", "restaurant_id" : "41255267" }
{ "_id" : ObjectId("5ab23ff163b071e80805e13b"), "borough" : "Brooklyn", "cuisine" : "American", "name" : "Willburg Cafe", "restaurant_id" : "41255152" }
{ "_id" : ObjectId("5ab23ff163b071e80805e70d"), "borough" : "Manhattan", "cuisine" : "Café/Coffee/Tea", "name" : "William Greenberg Jr Desserts", "restaurant_id" : "41353639" }
{ "_id" : ObjectId("5ab23ff163b071e80805e7fd"), "borough" : "Brooklyn", "cuisine" : "Vegetarian", "name" : "Wild Ginger Vegan Cafe", "restaurant_id" : "41369777" }
{ "_id" : ObjectId("5ab23ff163b071e80805e834"), "borough" : "Manhattan", "cuisine" : "Irish", "name" : "Wilfie & Nell", "restaurant_id" : "41372275" }
{ "_id" : ObjectId("5ab23ff163b071e80805ed48"), "borough" : "Manhattan", "cuisine" : "Vegetarian", "name" : "Wild Ginger Vegetarian Kitchen", "restaurant_id" : "41433964" }
{ "_id" : ObjectId("5ab23ff163b071e80805ee36"), "borough" : "Manhattan", "cuisine" : "Pizza", "name" : "Wild", "restaurant_id" : "41447856" }
{ "_id" : ObjectId("5ab23ff163b071e80805fa9b"), "borough" : "Manhattan", "cuisine" : "American", "name" : "William Barnacle", "restaurant_id" : "41591881" }
{ "_id" : ObjectId("5ab23ff163b071e80805fb1e"), "borough" : "Queens", "cuisine" : "American", "name" : "William Hallet", "restaurant_id" : "41597549" }
{ "_id" : ObjectId("5ab23ff163b071e80805fb3e"), "borough" : "Brooklyn", "cuisine" : "American", "name" : "Williams & Bailey", "restaurant_id" : "41598951" }
{ "_id" : ObjectId("5ab23ff163b071e80805fb6e"), "borough" : "Manhattan", "cuisine" : "Asian", "name" : "Wild Ginger", "restaurant_id" : "41600577" }
{ "_id" : ObjectId("5ab23ff163b071e80806032a"), "borough" : "Manhattan", "cuisine" : "Bakery", "name" : "William Greenberg Dessert", "restaurant_id" : "41657368" }
{ "_id" : ObjectId("5ab23ff163b071e8080604bb"), "borough" : "Brooklyn", "cuisine" : "Pizza/Italian", "name" : "Williamsburg Pizza", "restaurant_id" : "41672156" }
{ "_id" : ObjectId("5ab23ff163b071e808060514"), "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.