w3resource
w3resource logo

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("5ab23ff163b071e80805c958"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Pieces", "restaurant_id" : "40399910" }
{ "_id" : ObjectId("5ab23ff163b071e80805ca19"), "borough" : "Queens", "cuisine" : "American", "name" : "S.M.R Restaurant Services", "restaurant_id" : "40403857" }
{ "_id" : ObjectId("5ab23ff163b071e80805ca1c"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Good Shepherd Services", "restaurant_id" : "40403989" }
{ "_id" : ObjectId("5ab23ff163b071e80805ced0"), "borough" : "Queens", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "The Ice Box-Ralph'S Famous Italian Ices", "restaurant_id" : "40690899" }
{ "_id" : ObjectId("5ab23ff163b071e80805d0d4"), "borough" : "Brooklyn", "cuisine" : "Jewish/Kosher", "name" : "Alices", "restaurant_id" : "40782042" }
{ "_id" : ObjectId("5ab23ff163b071e80805d2f2"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Re: Sources", "restaurant_id" : "40876068" }
{ "_id" : ObjectId("5ab23ff163b071e80805d57a"), "borough" : "Staten Island", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Cange'S Italian Ices", "restaurant_id" : "40966018" }
{ "_id" : ObjectId("5ab23ff163b071e80805d636"), "borough" : "Brooklyn", "cuisine" : "American", "name" : "B.A.M. Cafe/Great Performances", "restaurant_id" : "40992170" }
{ "_id" : ObjectId("5ab23ff163b071e80805d7c6"), "borough" : "Brooklyn", "cuisine" : "American", "name" : "Barbara Blum Residence / Good Shepherd Services", "restaurant_id" : "41029591" }
{ "_id" : ObjectId("5ab23ff163b071e80805ddaf"), "borough" : "Brooklyn", "cuisine" : "Soul Food", "name" : "Berts Restaurant & Catering Services", "restaurant_id" : "41184590" }
{ "_id" : ObjectId("5ab23ff163b071e80805e12e"), "borough" : "Staten Island", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Famous Italian Ices", "restaurant_id" : "41254301" }
{ "_id" : ObjectId("5ab23ff163b071e80805e4fe"), "borough" : "Queens", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Famous Italian Ices", "restaurant_id" : "41316785" }
{ "_id" : ObjectId("5ab23ff163b071e80805e6aa"), "borough" : "Staten Island", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Famous Italian Ices", "restaurant_id" : "41347936" }
{ "_id" : ObjectId("5ab23ff163b071e80805e80d"), "borough" : "Manhattan", "cuisine" : "American", "name" : "International Flavors  & Fragrances", "restaurant_id" : "41370481" }
{ "_id" : ObjectId("5ab23ff163b071e80805eb8d"), "borough" : "Staten Island", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Ices", "restaurant_id" : "41410016" }
{ "_id" : ObjectId("5ab23ff163b071e80805ecb9"), "borough" : "Manhattan", "cuisine" : "American", "name" : "New York Stock Exchange Executive Services", "restaurant_id" : "41426518" }
{ "_id" : ObjectId("5ab23ff163b071e80805ed87"), "borough" : "Queens", "cuisine" : "Pizza", "name" : "Slices And Ices", "restaurant_id" : "41437112" }
{ "_id" : ObjectId("5ab23ff163b071e80805ef4c"), "borough" : "Staten Island", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Famous Italian Ices", "restaurant_id" : "41459709" }
{ "_id" : ObjectId("5ab23ff163b071e80805f8c6"), "borough" : "Manhattan", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Ralph'S Famous Italian Ices", "restaurant_id" : "41573883" }
{ "_id" : ObjectId("5ab23ff163b071e80805f9d7"), "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

Improve this sample solution and post your code through Disqus.



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