w3resource
w3resource logo

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("5ab23ff163b071e80805ca01"), "address" : { "coord" : [ -73.9860597, 40.7431194 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Madi
{ "_id" : ObjectId("5ab23ff163b071e80805cad0"), "address" : { "coord" : [ -73.98302199999999, 40.742313 ] }, "borough" : "Manhattan", "cuisine" : "Indian", "name" : "
{ "_id" : ObjectId("5ab23ff163b071e80805cd7e"), "address" : { "coord" : [ -74.000002, 40.72735 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Madame
{ "_id" : ObjectId("5ab23ff163b071e80805ce2e"), "address" : { "coord" : [ -73.98171959999999, 40.7499406 ] }, "borough" : "Manhattan", "cuisine" : "French", "name" :
{ "_id" : ObjectId("5ab23ff163b071e80805ceb7"), "address" : { "coord" : [ -73.9717845, 40.6897199 ] }, "borough" : "Brooklyn", "cuisine" : "African", "name" : "Madiba
{ "_id" : ObjectId("5ab23ff163b071e80805d1bd"), "address" : { "coord" : [ -73.9040753, 40.9069011 ] }, "borough" : "Bronx", "cuisine" : "Italian", "name" : "Madison'S
{ "_id" : ObjectId("5ab23ff163b071e80805d23b"), "address" : { "coord" : [ -73.9886598, 40.7565811 ] }, "borough" : "Manhattan", "cuisine" : "Hotdogs", "name" : "Madam
{ "_id" : ObjectId("5ab23ff163b071e80805d272"), "address" : { "coord" : [ -73.95623719999999, 40.7761697 ] }, "borough" : "Manhattan", "cuisine" : "American", "name"
{ "_id" : ObjectId("5ab23ff163b071e80805da2c"), "address" : { "coord" : [ -73.8885928, 40.8731713 ] }, "borough" : "Bronx", "cuisine" : "American", "name" : "Maddens
{ "_id" : ObjectId("5ab23ff163b071e80805dbf3"), "address" : { "coord" : [ -73.981973, 40.741028 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Mad Ha
{ "_id" : ObjectId("5ab23ff163b071e80805dd3f"), "address" : { "coord" : [ -73.8077582, 40.7633975 ] }, "borough" : "Queens", "cuisine" : "Korean", "name" : "Mad For C
{ "_id" : ObjectId("5ab23ff163b071e80805e1e9"), "address" : { "coord" : [ -73.9857545, 40.7498305 ] }, "borough" : "Manhattan", "cuisine" : "Korean", "name" : "Madang
{ "_id" : ObjectId("5ab23ff163b071e80805e420"), "address" : { "coord" : [ -73.97943400000001, 40.7521259 ] }, "borough" : "Manhattan", "cuisine" : "American", "name"
{ "_id" : ObjectId("5ab23ff163b071e80805e461"), "address" : { "coord" : [ -74.0103118, 40.7042077 ] }, "borough" : "Manhattan", "cuisine" : "Mexican", "name" : "Mad D
{ "_id" : ObjectId("5ab23ff163b071e80805e5ff"), "address" : { "coord" : [ -73.96974890000001, 40.64353699999999 ] }, "borough" : "Brooklyn", "cuisine" : "Indian", "na
{ "_id" : ObjectId("5ab23ff163b071e80805e840"), "address" : { "coord" : [ -74.002191, 40.7076992 ] }, "borough" : "Manhattan", "cuisine" : "Café/Coffee/Tea", "name" :
{ "_id" : ObjectId("5ab23ff163b071e80805e8aa"), "address" : { "coord" : [ -73.924184, 40.68904 ] }, "borough" : "Brooklyn", "cuisine" : "Pizza", "name" : "Maddy'S" }
{ "_id" : ObjectId("5ab23ff163b071e80805eafb"), "address" : { "coord" : [ -73.9650056, 40.7559881 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Madi
{ "_id" : ObjectId("5ab23ff163b071e80805f15f"), "address" : { "coord" : [ -73.95314379999999, 40.7445573 ] }, "borough" : "Queens", "cuisine" : "Latin (Cuban, Dominic
American)", "name" : "Madera Cuban Grill" }
{ "_id" : ObjectId("5ab23ff163b071e80805f921"), "address" : { "coord" : [ -73.976501, 40.7570304 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Madis
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.