w3resource

MongoDB Exercise - Find the restaurant name, borough, longitude and latitude and cuisine for those restaurants which contain Mad as first three letters of its name


Write a MongoDB query to find the restaurant name, borough, longitude and attitude and cuisine for those restaurants which contain 'Mad' as first three letters of 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 : 
                     { $regex : /^Mad/i, } 
                   },
                       {
                         "name":1,
                         "borough":1,
                         "address.coord":1,
                         "cuisine" :1
                        }
                   );

Output:

{ "_id" : ObjectId("564c2d949eb21ad392f17b04"), "address" : { "coord" : [ -73.9860597, 40.7431194 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Madison Square" }
{ "_id" : ObjectId("564c2d949eb21ad392f17bd5"), "address" : { "coord" : [ -73.98302199999999, 40.742313 ] }, "borough" : "Manhattan", "cuisine" : "Indian", "name" : "Madras Mahal" }
{ "_id" : ObjectId("564c2d949eb21ad392f17e82"), "address" : { "coord" : [ -74.000002, 40.72735 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Madame X" }
{ "_id" : ObjectId("564c2d949eb21ad392f17f31"), "address" : { "coord" : [ -73.98171959999999, 40.7499406 ] }, "borough" : "Manhattan", "cuisine" : "French", "name" : "Madison Bistro" }
{ "_id" : ObjectId("564c2d949eb21ad392f17fba"), "address" : { "coord" : [ -73.9717845, 40.6897199 ] }, "borough" : "Brooklyn", "cuisine" : "African", "name" : "Madiba" }
{ "_id" : ObjectId("564c2d949eb21ad392f182bf"), "address" : { "coord" : [ -73.9040753, 40.9069011 ] }, "borough" : "Bronx", "cuisine" : "Italian", "name" : "Madison'S" }
{ "_id" : ObjectId("564c2d949eb21ad392f1833d"), "address" : { "coord" : [ -73.9886598, 40.7565811 ] }, "borough" : "Manhattan", "cuisine" : "Hotdogs", "name" : "Madame Tussaud'S" }
{ "_id" : ObjectId("564c2d949eb21ad392f18375"), "address" : { "coord" : [ -73.95623719999999, 40.7761697 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Mad River Bar & Grille" }
{ "_id" : ObjectId("564c2d949eb21ad392f18b2c"), "address" : { "coord" : [ -73.8885928, 40.8731713 ] }, "borough" : "Bronx", "cuisine" : "American ", "name" : "Maddens Pub" }
{ "_id" : ObjectId("564c2d949eb21ad392f18cf4"), "address" : { "coord" : [ -73.981973, 40.741028 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Mad Hatter Saloon" }
{ "_id" : ObjectId("564c2d949eb21ad392f18e3f"), "address" : { "coord" : [ -73.8077582, 40.7633975 ] }, "borough" : "Queens", "cuisine" : "Korean", "name" : "Mad For Chicken" }
{ "_id" : ObjectId("564c2d949eb21ad392f192e7"), "address" : { "coord" : [ -73.9857545, 40.7498305 ] }, "borough" : "Manhattan", "cuisine" : "Korean", "name" : "Madangsui" }
{ "_id" : ObjectId("564c2d949eb21ad392f1951d"), "address" : { "coord" : [ -73.97943400000001, 40.7521259 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Madison & Vine" }
{ "_id" : ObjectId("564c2d949eb21ad392f1955f"), "address" : { "coord" : [ -74.0103118, 40.7042077 ] }, "borough" : "Manhattan", "cuisine" : "Mexican", "name" : "Mad Dog & Beans" }
{ "_id" : ObjectId("564c2d949eb21ad392f196fc"), "address" : { "coord" : [ -73.96974890000001, 40.64353699999999 ] }, "borough" : "Brooklyn", "cuisine" : "Indian", "name" : "Madina Restaurant" }
{ "_id" : ObjectId("564c2d949eb21ad392f1993c"), "address" : { "coord" : [ -74.002191, 40.7076992 ] }, "borough" : "Manhattan", "cuisine" : "Café/Coffee/Tea", "name" : "Made Fresh Daily" }
{ "_id" : ObjectId("564c2d949eb21ad392f199a6"), "address" : { "coord" : [ -73.924184, 40.68904 ] }, "borough" : "Brooklyn", "cuisine" : "Pizza", "name" : "Maddy'S" }
{ "_id" : ObjectId("564c2d949eb21ad392f19bf7"), "address" : { "coord" : [ -73.9650056, 40.7559881 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Madison Restaurant" }
{ "_id" : ObjectId("564c2d949eb21ad392f1a258"), "address" : { "coord" : [ -73.95314379999999, 40.7445573 ] }, "borough" : "Queens", "cuisine" : "Latin (Cuban, Dominican, Puerto Rican, South & Central American)", "name" : "Madera Cuban Grill" }
{ "_id" : ObjectId("564c2d949eb21ad392f1aa1a"), "address" : { "coord" : [ -73.976501, 40.7570304 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Madison Deli" }
Type "it" for more

Explanation:

The said query in MongoDB that searches for a list of all restaurants where the name field starts with "Mad" (case-insensitive), along with their respective name, borough, address.coord, and cuisine field values.
The $regex regular expression works on the name field and here the /^Mad/i pattern, which matches any string that starts with "Mad" (case-insensitive).

Note: This output is generated using MongoDB server version 3.6

Improve this sample solution and post your code through Disqus.

Previous: Find the restaurant's name, borough, longitude, attitude and cuisine if the restaurant contains three letters 'mon'.
Next: Restaurants with less than 5 grades.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.