w3resource

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("564c2d939eb21ad392f1765d"), "address" : { "coord" : [ -73.98306099999999, 40.7441419 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Desmond'S Tavern" }
{ "_id" : ObjectId("564c2d939eb21ad392f17666"), "address" : { "coord" : [ -73.8221418, 40.7272376 ] }, "borough" : "Queens", "cuisine" : "Jewish/Kosher", "name" : "Shimons Kosher Pizza" }
{ "_id" : ObjectId("564c2d939eb21ad392f17672"), "address" : { "coord" : [ -74.10465599999999, 40.58834 ] }, "borough" : "Staten Island", "cuisine" : "American ", "name" : "Richmond County Country Club" }
{ "_id" : ObjectId("564c2d939eb21ad392f176a7"), "address" : { "coord" : [ -73.9812843, 40.5947365 ] }, "borough" : "Brooklyn", "cuisine" : "Pizza/Italian", "name" : "Lb Spumoni Gardens" }
{ "_id" : ObjectId("564c2d939eb21ad392f176ee"), "address" : { "coord" : [ -73.951199, 40.7166026 ] }, "borough" : "Brooklyn", "cuisine" : "Italian", "name" : "Bamonte'S Restaurant" }
{ "_id" : ObjectId("564c2d939eb21ad392f17725"), "address" : { "coord" : [ -73.924072, 40.76108900000001 ] }, "borough" : "Queens", "cuisine" : "Greek", "name" : "Omonia Cafe" }
{ "_id" : ObjectId("564c2d939eb21ad392f17775"), "address" : { "coord" : [ -74.0023353, 40.7333573 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Manhattan Monster" }
{ "_id" : ObjectId("564c2d939eb21ad392f177a3"), "address" : { "coord" : [ -74.001094, 40.729583 ] }, "borough" : "Manhattan", "cuisine" : "Italian", "name" : "Monte'S" }
{ "_id" : ObjectId("564c2d939eb21ad392f177a8"), "address" : { "coord" : [ -73.9901605, 40.7526176 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Delmonico'S Kitchen" }
{ "_id" : ObjectId("564c2d939eb21ad392f177be"), "address" : { "coord" : [ -73.9979536, 40.6914024 ] }, "borough" : "Brooklyn", "cuisine" : "Bottled beverages, including water, sodas, juices, etc.", "name" : "Montero Bar & Grill" }
{ "_id" : ObjectId("564c2d939eb21ad392f17949"), "address" : { "coord" : [ -73.9707505, 40.7635651 ] }, "borough" : "Manhattan", "cuisine" : "Delicatessen", "name" : "Delmonico Gourmet" }
{ "_id" : ObjectId("564c2d949eb21ad392f179b9"), "address" : { "coord" : [ -73.9760637, 40.7508686 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Delmonico Gourmet" }
{ "_id" : ObjectId("564c2d949eb21ad392f179c9"), "address" : { "coord" : [ -73.9705633, 40.7605759 ] }, "borough" : "Manhattan", "cuisine" : "Italian", "name" : "Montebello Restaurant" }
{ "_id" : ObjectId("564c2d949eb21ad392f17a62"), "address" : { "coord" : [ -74.1914658, 40.55274360000001 ] }, "borough" : "Staten Island", "cuisine" : "Italian", "name" : "Villa Monte Pizzeria" }
{ "_id" : ObjectId("564c2d949eb21ad392f17b0d"), "address" : { "coord" : [ -73.97198209999999, 40.764464 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Harmonie Club" }
{ "_id" : ObjectId("564c2d949eb21ad392f17bd2"), "address" : { "coord" : [ -73.79571990000001, 40.7095637 ] }, "borough" : "Queens", "cuisine" : "Bakery", "name" : "Ramona'S Bakery" }
{ "_id" : ObjectId("564c2d949eb21ad392f17c06"), "address" : { "coord" : [ -73.97852100000001, 40.729192 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Mona'S" }
{ "_id" : ObjectId("564c2d949eb21ad392f17c2b"), "address" : { "coord" : [ -73.98463480000001, 40.7630755 ] }, "borough" : "Manhattan", "cuisine" : "American ", "name" : "Neil Simon Theatre" }
{ "_id" : ObjectId("564c2d949eb21ad392f17c68"), "address" : { "coord" : [ -73.9938361, 40.6091317 ] }, "borough" : "Brooklyn", "cuisine" : "Bakery", "name" : "Mondial Bakery" }
{ "_id" : ObjectId("564c2d949eb21ad392f17c8c"), "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

Explanation:

The said query in MongoDB query that searches for a list of all restaurants where the name field starts with "mon" (case-insensitive), along with their respective name, borough, address.coord, and cuisine field values.
The $regex regular expression that matches any string that starts with "mon" in the name field and the $options: "i" is specifies that the case-insensitive option for the regular expression.

Improve this sample solution and post your code through Disqus.

Previous: The Id, name and grades of those restaurants with a remainder of 0 after dividing the score by 7 should be selected.
Next: Find the restaurant name, borough, longitude, attitude and cuisine for those restaurants with 'Mad' in their first three letters.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb-exercises/mongodb-exercise-31.php