w3resource
w3resource logo

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("5ab23ff163b071e80805c4cb"), "borough" : "Brooklyn", "cuisine" : "American", "name" : "Regina Caterers", "restaurant_id" : "40356649" }
{ "_id" : ObjectId("5ab23ff163b071e80805c5c8"), "borough" : "Manhattan", "cuisine" : "Café/Coffee/Tea", "name" : "Caffe Reggio", "restaurant_id" : "40369418" }
{ "_id" : ObjectId("5ab23ff163b071e80805c6d8"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Regency Hotel", "restaurant_id" : "40382679" }
{ "_id" : ObjectId("5ab23ff163b071e80805c9f5"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Regency Whist Club", "restaurant_id" : "40402377" }
{ "_id" : ObjectId("5ab23ff163b071e80805cad8"), "borough" : "Queens", "cuisine" : "American", "name" : "Rego Park Cafe", "restaurant_id" : "40523342" }
{ "_id" : ObjectId("5ab23ff163b071e80805d14b"), "borough" : "Queens", "cuisine" : "Pizza", "name" : "Regina Pizza", "restaurant_id" : "40801325" }
{ "_id" : ObjectId("5ab23ff163b071e80805d360"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Regal Entertainment Group", "restaurant_id" : "40891782" }
{ "_id" : ObjectId("5ab23ff163b071e80805d6a3"), "borough" : "Brooklyn", "cuisine" : "Café/Coffee/Tea", "name" : "Cafe Regular", "restaurant_id" : "41003435" }
{ "_id" : ObjectId("5ab23ff163b071e80805d87d"), "borough" : "Queens", "cuisine" : "Middle Eastern", "name" : "Rego Pita", "restaurant_id" : "41053746" }
{ "_id" : ObjectId("5ab23ff163b071e80805da09"), "borough" : "Manhattan", "cuisine" : "Italian", "name" : "Regional", "restaurant_id" : "41096822" }
{ "_id" : ObjectId("5ab23ff163b071e80805ddac"), "borough" : "Queens", "cuisine" : "American", "name" : "Regal Cinema 8 (Atlas Park Stadium)", "restaurant_id" : "41184464" }
{ "_id" : ObjectId("5ab23ff163b071e80805df83"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Regal Cinemas E-Walk Stadium13", "restaurant_id" : "41227884" }
{ "_id" : ObjectId("5ab23ff163b071e80805e4b2"), "borough" : "Queens", "cuisine" : "Jewish/Kosher", "name" : "Regestan", "restaurant_id" : "41312517" }
{ "_id" : ObjectId("5ab23ff163b071e80805e602"), "borough" : "Brooklyn", "cuisine" : "American", "name" : "Sheepshead Bay Regal Cinemas Theatre", "restaurant_id" : "41335396" }
{ "_id" : ObjectId("5ab23ff163b071e80805eb06"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Regal Union Square Stadium 14", "restaurant_id" : "41402060" }
{ "_id" : ObjectId("5ab23ff163b071e80805ec40"), "borough" : "Manhattan", "cuisine" : "American", "name" : "St Regis New York - 20Th Floor Roof Ballroom", "restaurant_id" : "41420313" }
{ "_id" : ObjectId("5ab23ff163b071e80805ed0e"), "borough" : "Queens", "cuisine" : "Chinese", "name" : "Rego Garden Restaurant, Inc", "restaurant_id" : "41430576" }
{ "_id" : ObjectId("5ab23ff163b071e80805f1d1"), "borough" : "Queens", "cuisine" : "Pizza/Italian", "name" : "Regina'S Cafe & Pizzeria", "restaurant_id" : "41486945" }
{ "_id" : ObjectId("5ab23ff163b071e80805f6c9"), "borough" : "Queens", "cuisine" : "American", "name" : "Rego Bagel", "restaurant_id" : "41553722" }
{ "_id" : ObjectId("5ab23ff163b071e80805f8fc"), "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

Improve this sample solution and post your code through Disqus.



Amazon promo codes to get huge discounts for limited period (USA only).