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

Explanation:

The said query in MongoDB that finds all documents in the "restaurants" collection that is looking for restaurants whose name starts with "Wil", and then returning information about each restaurant's ID, name, borough, and cuisine.
The {"name": /^Wil/} filters the documents where the value of the "name" field starts with the letters "Wil".
The {"restaurant_id" : 1, "name":1, "borough":1, "cuisine" :1} have been used to include the "restaurant_id", "name", "borough", and "cuisine" fields. The value 1 is used for each field to include that field in the results.

Improve this sample solution and post your code through Disqus.

Previous: Identify the restaurants that do not serve American cuisine and achieve a grade point 'A' outside of Brooklyn.
Next: Find restaurant names, addresses, and cuisines with 'CES' as their last three letters.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb-exercises/mongodb-exercise-14.php