w3resource
w3resource logo

MongoDB Exercise - Find the restaurant name, borough, longitude and latitude and cuisine for those restaurants which contains mon in its name


Write a MongoDB query to find the restaurant name, borough, longitude and attitude and cuisine for those restaurants which contains 'mon' 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 : 
                     { $regex : "mon.*", $options: "i" } 
                   },
                       {
                         "name":1,
                         "borough":1,
                         "address.coord":1,
                         "cuisine" :1
                        }
                   );

Output:

{ "_id" : ObjectId("5ab23ff163b071e80805c557"), "address" : { "coord" : [ -73.98306099999999, 40.7441419 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Desmond'S Tavern" }
{ "_id" : ObjectId("5ab23ff163b071e80805c560"), "address" : { "coord" : [ -73.8221418, 40.7272376 ] }, "borough" : "Queens", "cuisine" : "Jewish/Kosher", "name" : "Shimons Kosher Pizza" }
{ "_id" : ObjectId("5ab23ff163b071e80805c56c"), "address" : { "coord" : [ -74.10465599999999, 40.58834 ] }, "borough" : "Staten Island", "cuisine" : "American", "name" : "Richmond County Country Club"
 }
{ "_id" : ObjectId("5ab23ff163b071e80805c598"), "address" : { "coord" : [ -73.9812843, 40.5947365 ] }, "borough" : "Brooklyn", "cuisine" : "Pizza/Italian", "name" : "Lb Spumoni Gardens" }
{ "_id" : ObjectId("5ab23ff163b071e80805c5ea"), "address" : { "coord" : [ -73.951199, 40.7166026 ] }, "borough" : "Brooklyn", "cuisine" : "Italian", "name" : "Bamonte'S Restaurant" }
{ "_id" : ObjectId("5ab23ff163b071e80805c621"), "address" : { "coord" : [ -73.924072, 40.76108900000001 ] }, "borough" : "Queens", "cuisine" : "Greek", "name" : "Omonia Cafe" }
{ "_id" : ObjectId("5ab23ff163b071e80805c671"), "address" : { "coord" : [ -74.0023353, 40.7333573 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Manhattan Monster" }
{ "_id" : ObjectId("5ab23ff163b071e80805c69e"), "address" : { "coord" : [ -74.001094, 40.729583 ] }, "borough" : "Manhattan", "cuisine" : "Italian", "name" : "Monte'S" }
{ "_id" : ObjectId("5ab23ff163b071e80805c6a4"), "address" : { "coord" : [ -73.9901605, 40.7526176 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Delmonico'S Kitchen" }
{ "_id" : ObjectId("5ab23ff163b071e80805c6b9"), "address" : { "coord" : [ -73.9979536, 40.6914024 ] }, "borough" : "Brooklyn", "cuisine" : "Bottled beverages, including water, sodas, juices, etc.", "n
ame" : "Montero Bar & Grill" }
{ "_id" : ObjectId("5ab23ff163b071e80805c845"), "address" : { "coord" : [ -73.9707505, 40.7635651 ] }, "borough" : "Manhattan", "cuisine" : "Delicatessen", "name" : "Delmonico Gourmet" }
{ "_id" : ObjectId("5ab23ff163b071e80805c8b2"), "address" : { "coord" : [ -73.9760637, 40.7508686 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Delmonico Gourmet" }
{ "_id" : ObjectId("5ab23ff163b071e80805c8c7"), "address" : { "coord" : [ -73.9705633, 40.7605759 ] }, "borough" : "Manhattan", "cuisine" : "Italian", "name" : "Montebello Restaurant" }
{ "_id" : ObjectId("5ab23ff163b071e80805c95e"), "address" : { "coord" : [ -74.1914658, 40.55274360000001 ] }, "borough" : "Staten Island", "cuisine" : "Italian", "name" : "Villa Monte Pizzeria" }
{ "_id" : ObjectId("5ab23ff163b071e80805ca09"), "address" : { "coord" : [ -73.97198209999999, 40.764464 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Harmonie Club" }
{ "_id" : ObjectId("5ab23ff163b071e80805cace"), "address" : { "coord" : [ -73.79571990000001, 40.7095637 ] }, "borough" : "Queens", "cuisine" : "Bakery", "name" : "Ramona'S Bakery" }
{ "_id" : ObjectId("5ab23ff163b071e80805cb02"), "address" : { "coord" : [ -73.97852100000001, 40.729192 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Mona'S" }
{ "_id" : ObjectId("5ab23ff163b071e80805cb27"), "address" : { "coord" : [ -73.98463480000001, 40.7630755 ] }, "borough" : "Manhattan", "cuisine" : "American", "name" : "Neil Simon Theatre" }
{ "_id" : ObjectId("5ab23ff163b071e80805cb62"), "address" : { "coord" : [ -73.9938361, 40.6091317 ] }, "borough" : "Brooklyn", "cuisine" : "Bakery", "name" : "Mondial Bakery" }
{ "_id" : ObjectId("5ab23ff163b071e80805cb88"), "address" : { "coord" : [ -74.028486, 40.630438 ] }, "borough" : "Brooklyn", "cuisine" : "Mediterranean", "name" : "Omonia Cafe" }
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.