MongoDB Exercise - Find the restaurant Id, name, borough and cuisine for those restaurants which contain ces as last three letters for its name
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.
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: /ces$/},
{
"restaurant_id" : 1,
"name":1,"borough":1,
"cuisine" :1
}
);
Output:
{ "_id" : ObjectId("564c2d949eb21ad392f17a5b"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "Pieces", "restaurant_id" : "40399910" } { "_id" : ObjectId("564c2d949eb21ad392f17b1b"), "borough" : "Queens", "cuisine" : "American ", "name" : "S.M.R Restaurant Services", "restaurant_id" : "40403857" } { "_id" : ObjectId("564c2d949eb21ad392f17b21"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "Good Shepherd Services", "restaurant_id" : "40403989" } { "_id" : ObjectId("564c2d949eb21ad392f17fd3"), "borough" : "Queens", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "The Ice Box-Ralph'S Famous Italian Ices", "restaurant_id" : "40690899" } { "_id" : ObjectId("564c2d949eb21ad392f181d6"), "borough" : "Brooklyn", "cuisine" : "Jewish/Kosher", "name" : "Alices", "restaurant_id" : "40782042" } { "_id" : ObjectId("564c2d949eb21ad392f183f0"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "Re: Sources", "restaurant_id" : "40876068" } { "_id" : ObjectId("564c2d949eb21ad392f1867c"), "borough" : "Staten Island", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Cange'S Italian Ices", "restaurant_id" : "40966018" } { "_id" : ObjectId("564c2d949eb21ad392f18735"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "B.A.M. Cafe/Great Performances", "restaurant_id" : "40992170" } { "_id" : ObjectId("564c2d949eb21ad392f188c7"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "Barbara Blum Residence / Good Shepherd Services", "restaurant_id" : "41029591" } { "_id" : ObjectId("564c2d949eb21ad392f18eae"), "borough" : "Brooklyn", "cuisine" : "Soul Food", "name" : "Berts Restaurant & Catering Services", "restaurant_id" : "41184590" } { "_id" : ObjectId("564c2d949eb21ad392f1922c"), "borough" : "Staten Island", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Famous Italian Ices", "restaurant_id" : "41254301" } { "_id" : ObjectId("564c2d949eb21ad392f195fa"), "borough" : "Queens", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Famous Italian Ices", "restaurant_id" : "41316785" } { "_id" : ObjectId("564c2d949eb21ad392f197a6"), "borough" : "Staten Island", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Famous Italian Ices", "restaurant_id" : "41347936" } { "_id" : ObjectId("564c2d949eb21ad392f19908"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "International Flavors & Fragrances", "restaurant_id" : "41370481" } { "_id" : ObjectId("564c2d949eb21ad392f19c8a"), "borough" : "Staten Island", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Ices", "restaurant_id" : "41410016" } { "_id" : ObjectId("564c2d949eb21ad392f19db3"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "New York Stock Exchange Executive Services", "restaurant_id" : "41426518" } { "_id" : ObjectId("564c2d949eb21ad392f19e82"), "borough" : "Queens", "cuisine" : "Pizza", "name" : "Slices And Ices", "restaurant_id" : "41437112" } { "_id" : ObjectId("564c2d949eb21ad392f1a046"), "borough" : "Staten Island", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Famous Italian Ices", "restaurant_id" : "41459709" } { "_id" : ObjectId("564c2d949eb21ad392f1a9bb"), "borough" : "Manhattan", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Famous Italian Ices", "restaurant_id" : "41573883" } { "_id" : ObjectId("564c2d949eb21ad392f1aacf"), "borough" : "Bronx", "cuisine" : "Caribbean", "name" : "7 Spices", "restaurant_id" : "41584120" } Type "it" for more
Note: This output is generated using MongoDB server version 3.6
Explanation:
The said query in MongoDB that returns all documents in the 'restaurants' collection where the value of the "name" field ends with "ces".
The argument {"restaurant_id" : 1, "name":1, "borough":1, "cuisine" :1} of the "find" method includes "restaurant_id", "name", "borough", and "cuisine" field in the returned documents, where 1 means to include the field and 0 means to exclude the field.
Improve this sample solution and post your code through Disqus.
Previous: Restaurants with 'Wil' as its first three letters can be identified by their Id, name, borough, and cuisine.
Next: Restaurant names that contain 'Reg' as three letters, restaurant Ids, boroughs, and cuisines.
What is the difficulty level of this exercise?
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://www.w3resource.com/mongodb-exercises/mongodb-exercise-15.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics