MongoDB Exercise - Find the restaurant Id, name, borough and cuisine for those restaurants which contain Reg as three letters somewhere in its name
Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which contain 'Reg' as three letters somewhere in 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": /.*Reg.*/},
{
"restaurant_id" : 1,
"name":1,"borough":1,
"cuisine" :1
}
);
Output:
{ "_id" : ObjectId("564c2d939eb21ad392f175cf"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "Regina Caterers", "restaurant_id" : "40356649" } { "_id" : ObjectId("564c2d939eb21ad392f176ce"), "borough" : "Manhattan", "cuisine" : "Café/Coffee/Tea", "name" : "Caffe Reggio", "restaurant_id" : "40369418" } { "_id" : ObjectId("564c2d939eb21ad392f177dd"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "Regency Hotel", "restaurant_id" : "40382679" } { "_id" : ObjectId("564c2d949eb21ad392f17afa"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "Regency Whist Club", "restaurant_id" : "40402377" } { "_id" : ObjectId("564c2d949eb21ad392f17bdd"), "borough" : "Queens", "cuisine" : "American ", "name" : "Rego Park Cafe", "restaurant_id" : "40523342" } { "_id" : ObjectId("564c2d949eb21ad392f1824b"), "borough" : "Queens", "cuisine" : "Pizza", "name" : "Regina Pizza", "restaurant_id" : "40801325" } { "_id" : ObjectId("564c2d949eb21ad392f18462"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "Regal Entertainment Group", "restaurant_id" : "40891782" } { "_id" : ObjectId("564c2d949eb21ad392f187a4"), "borough" : "Brooklyn", "cuisine" : "Café/Coffee/Tea", "name" : "Cafe Regular", "restaurant_id" : "41003435" } { "_id" : ObjectId("564c2d949eb21ad392f1897c"), "borough" : "Queens", "cuisine" : "Middle Eastern", "name" : "Rego Pita", "restaurant_id" : "41053746" } { "_id" : ObjectId("564c2d949eb21ad392f18b09"), "borough" : "Manhattan", "cuisine" : "Italian", "name" : "Regional", "restaurant_id" : "41096822" } { "_id" : ObjectId("564c2d949eb21ad392f18eab"), "borough" : "Queens", "cuisine" : "American ", "name" : "Regal Cinema 8 (Atlas Park Stadium)", "restaurant_id" : "41184464" } { "_id" : ObjectId("564c2d949eb21ad392f19083"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "Regal Cinemas E-Walk Stadium13", "restaurant_id" : "41227884" } { "_id" : ObjectId("564c2d949eb21ad392f195ae"), "borough" : "Queens", "cuisine" : "Jewish/Kosher", "name" : "Regestan", "restaurant_id" : "41312517" } { "_id" : ObjectId("564c2d949eb21ad392f196ff"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "Sheepshead Bay Regal Cinemas Theatre", "restaurant_id" : "41335396" } { "_id" : ObjectId("564c2d949eb21ad392f19c02"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "Regal Union Square Stadium 14", "restaurant_id" : "41402060" } { "_id" : ObjectId("564c2d949eb21ad392f19d3c"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "St Regis New York - 20Th Floor Roof Ballroom", "restaurant_id" : "41420313" } { "_id" : ObjectId("564c2d949eb21ad392f19e09"), "borough" : "Queens", "cuisine" : "Chinese", "name" : "Rego Garden Restaurant, Inc", "restaurant_id" : "41430576" } { "_id" : ObjectId("564c2d949eb21ad392f1a2c9"), "borough" : "Queens", "cuisine" : "Pizza/Italian", "name" : "Regina'S Cafe & Pizzeria", "restaurant_id" : "41486945" } { "_id" : ObjectId("564c2d949eb21ad392f1a7c3"), "borough" : "Queens", "cuisine" : "American ", "name" : "Rego Bagel", "restaurant_id" : "41553722" } { "_id" : ObjectId("564c2d949eb21ad392f1a9f4"), "borough" : "Queens", "cuisine" : "Caribbean", "name" : "Reggae Food Restaurant & Lounge", "restaurant_id" : "41575236" } Type "it" for more
Note: This output is generated using MongoDB server version 3.6
Explanation:
The said query in MongoDB that searches for all documents in the "restaurants" collection where the "name" field matches a regular expression pattern that includes the substring "Reg", and returns a subset of fields that is "restaurant_id", "name", "borough", and "cuisine" for each matching document.
The regular expression pattern used in the query is "/.Reg./", which matches any string that contains the substring "Reg" anywhere in it.
Improve this sample solution and post your code through Disqus.
Previous: Find restaurant names, addresses, and cuisines with 'CES' as their last three letters.
Next: Find restaurants in the Bronx that prepare American or Chinese food.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics