w3resource
w3resource logo

MongoDB Exercise - Find the restaurant Id, name, address and geographical location of the restaurants where 2nd element contains a value between 42 and 52


Write a MongoDB query to find the restaurant Id, name, address and geographical location for those restaurants where 2nd element of coord array contains a value which is more than 42 and upto 52.

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( 
                      { 
                        "address.coord.1": {$gt : 42, $lte : 52}
                      },
                        {"restaurant_id" : 1,"name":1,"address":1,"coord":1}
                   );

Output:

{ "_id" : ObjectId("5ab23ff163b071e80805c765"), "address" : { "building" : "47", "coord" : [ -78.877224, 42.89546199999999 ], "street" : "Broadway @ Trinity Pl", "zipcode" : "10006" }, "name" : "T.G.I
. Friday'S", "restaurant_id" : "40387990" }
{ "_id" : ObjectId("5ab23ff163b071e80805c792"), "address" : { "building" : "1", "coord" : [ -0.7119979, 51.6514664 ], "street" : "Pennplaza E, Penn Sta", "zipcode" : "10001" }, "name" : "T.G.I. Friday
s", "restaurant_id" : "40388936" }
{ "_id" : ObjectId("5ab23ff163b071e80805c9ed"), "address" : { "building" : "3000", "coord" : [ -87.86567699999999, 42.61150920000001 ], "street" : "47 Avenue", "zipcode" : "11101" }, "name" : "Di Luvi
o'S Deli", "restaurant_id" : "40402284" }
{ "_id" : ObjectId("5ab23ff163b071e80805cc24"), "address" : { "building" : "21972199", "coord" : [ -78.589606, 42.8912372 ], "street" : "Broadway", "zipcode" : "10024" }, "name" : "La Caridad 78", "re
staurant_id" : "40568285" }
{ "_id" : ObjectId("5ab23ff163b071e80805d2f5"), "address" : { "building" : "7981", "coord" : [ -84.9751215, 45.4713351 ], "street" : "Hoyt Street", "zipcode" : "11201" }, "name" : "Bijan'S", "restaura
nt_id" : "40876618" }
{ "_id" : ObjectId("5ab23ff163b071e80805d30a"), "address" : { "building" : "0", "coord" : [ -88.0778799, 42.4154769 ], "street" : "& Grand Central", "zipcode" : "10017" }, "name" : "Hyatt, Ny Central/
Room Service", "restaurant_id" : "40879243" }
{ "_id" : ObjectId("5ab23ff163b071e80805d320"), "address" : { "building" : "60", "coord" : [ -111.9975205, 42.0970258 ], "street" : "West Side Highway", "zipcode" : "10006" }, "name" : "Sports Center
At Chelsea Piers (Sushi Bar)", "restaurant_id" : "40882356" }
{ "_id" : ObjectId("5ab23ff163b071e80805d89d"), "address" : { "building" : "1", "coord" : [ -72.4751457, 43.2956803 ], "street" : "Wall Street Court", "zipcode" : "10005" }, "name" : "Fino Ristorante"
, "restaurant_id" : "41057286" }
{ "_id" : ObjectId("5ab23ff163b071e80805d928"), "address" : { "building" : "350", "coord" : [ -93.2069217, 43.14769159999999 ], "street" : "5 Avenue", "zipcode" : "10018" }, "name" : "Heartland Brewer
y", "restaurant_id" : "41073985" }
{ "_id" : ObjectId("5ab23ff163b071e80805dbab"), "address" : { "building" : "0", "coord" : [ -73.66037349999999, 42.4476886 ], "street" : "Concourse B", "zipcode" : "11371" }, "name" : "Dunkin' Donuts"
, "restaurant_id" : "41142675" }
{ "_id" : ObjectId("5ab23ff163b071e80805dbbd"), "address" : { "building" : "1342", "coord" : [ -73.8162457, 42.7025194 ], "street" : "Central Avenue", "zipcode" : "11691" }, "name" : "New Beijing Rest
aurant", "restaurant_id" : "41144626" }
{ "_id" : ObjectId("5ab23ff163b071e80805df44"), "address" : { "building" : "350", "coord" : [ -93.2069217, 43.14769159999999 ], "street" : "5 Avenue", "zipcode" : "10018" }, "name" : "Chipotle Mexican
 Grill", "restaurant_id" : "41223834" }
{ "_id" : ObjectId("5ab23ff163b071e80805df9c"), "address" : { "building" : "350", "coord" : [ -93.2069217, 43.14769159999999 ], "street" : "5 Avenue", "zipcode" : "10018" }, "name" : "Europa Cafe", "r
estaurant_id" : "41229799" }
{ "_id" : ObjectId("5ab23ff163b071e80805e1db"), "address" : { "building" : "350", "coord" : [ -93.2069217, 43.14769159999999 ], "street" : "5 Avenue", "zipcode" : "10018" }, "name" : "Ny Skyride", "re
staurant_id" : "41266025" }
{ "_id" : ObjectId("5ab23ff163b071e80805e3a8"), "address" : { "building" : "1", "coord" : [ -72.4751457, 43.2956803 ], "street" : "Wall Street Court", "zipcode" : "10005" }, "name" : "Haru", "restaura
nt_id" : "41298810" }
{ "_id" : ObjectId("5ab23ff163b071e80805e4ab"), "address" : { "building" : "1618", "coord" : [ -77.6873462, 43.2236789 ], "street" : "Maiden Lane", "zipcode" : "10038" }, "name" : "Oka Sushi", "restau
rant_id" : "41312239" }
{ "_id" : ObjectId("5ab23ff163b071e80805e66b"), "address" : { "building" : "7072", "coord" : [ -85.70723199999999, 42.3625186 ], "street" : "North    6 Street", "zipcode" : "11211" }, "name" : "Black
Bear Bar", "restaurant_id" : "41343482" }
{ "_id" : ObjectId("5ab23ff163b071e80805e6b5"), "address" : { "building" : "350", "coord" : [ -93.2069217, 43.14769159999999 ], "street" : "5 Avenue", "zipcode" : "10018" }, "name" : "Starbucks Coffee
", "restaurant_id" : "41348358" }
{ "_id" : ObjectId("5ab23ff163b071e80805e7d8"), "address" : { "building" : "0", "coord" : [ 0.5595998999999999, 51.3940452 ], "street" : "Pier 78 West 38 Street", "zipcode" : "10018" }, "name" : "Pier
 Side Cafe", "restaurant_id" : "41367417" }
 { "_id" : ObjectId("5ab23ff163b071e80805c765"), "address" : { "building" : "47", "coord" : [ -78.877224, 42.89546199999999 ], "street" : "Broadway @ Trinity Pl", "zipcode" : "10006" }, "name" : "T.G.I
. Friday'S", "restaurant_id" : "40387990" }
{ "_id" : ObjectId("5ab23ff163b071e80805c792"), "address" : { "building" : "1", "coord" : [ -0.7119979, 51.6514664 ], "street" : "Pennplaza E, Penn Sta", "zipcode" : "10001" }, "name" : "T.G.I. Friday
s", "restaurant_id" : "40388936" }
{ "_id" : ObjectId("5ab23ff163b071e80805c9ed"), "address" : { "building" : "3000", "coord" : [ -87.86567699999999, 42.61150920000001 ], "street" : "47 Avenue", "zipcode" : "11101" }, "name" : "Di Luvi
o'S Deli", "restaurant_id" : "40402284" }
{ "_id" : ObjectId("5ab23ff163b071e80805cc24"), "address" : { "building" : "21972199", "coord" : [ -78.589606, 42.8912372 ], "street" : "Broadway", "zipcode" : "10024" }, "name" : "La Caridad 78", "re
staurant_id" : "40568285" }
{ "_id" : ObjectId("5ab23ff163b071e80805d2f5"), "address" : { "building" : "7981", "coord" : [ -84.9751215, 45.4713351 ], "street" : "Hoyt Street", "zipcode" : "11201" }, "name" : "Bijan'S", "restaura
nt_id" : "40876618" }
{ "_id" : ObjectId("5ab23ff163b071e80805d30a"), "address" : { "building" : "0", "coord" : [ -88.0778799, 42.4154769 ], "street" : "& Grand Central", "zipcode" : "10017" }, "name" : "Hyatt, Ny Central/
Room Service", "restaurant_id" : "40879243" }
{ "_id" : ObjectId("5ab23ff163b071e80805d320"), "address" : { "building" : "60", "coord" : [ -111.9975205, 42.0970258 ], "street" : "West Side Highway", "zipcode" : "10006" }, "name" : "Sports Center
At Chelsea Piers (Sushi Bar)", "restaurant_id" : "40882356" }
{ "_id" : ObjectId("5ab23ff163b071e80805d89d"), "address" : { "building" : "1", "coord" : [ -72.4751457, 43.2956803 ], "street" : "Wall Street Court", "zipcode" : "10005" }, "name" : "Fino Ristorante"
, "restaurant_id" : "41057286" }
{ "_id" : ObjectId("5ab23ff163b071e80805d928"), "address" : { "building" : "350", "coord" : [ -93.2069217, 43.14769159999999 ], "street" : "5 Avenue", "zipcode" : "10018" }, "name" : "Heartland Brewer
y", "restaurant_id" : "41073985" }
{ "_id" : ObjectId("5ab23ff163b071e80805dbab"), "address" : { "building" : "0", "coord" : [ -73.66037349999999, 42.4476886 ], "street" : "Concourse B", "zipcode" : "11371" }, "name" : "Dunkin' Donuts"
, "restaurant_id" : "41142675" }
{ "_id" : ObjectId("5ab23ff163b071e80805dbbd"), "address" : { "building" : "1342", "coord" : [ -73.8162457, 42.7025194 ], "street" : "Central Avenue", "zipcode" : "11691" }, "name" : "New Beijing Rest
aurant", "restaurant_id" : "41144626" }
{ "_id" : ObjectId("5ab23ff163b071e80805df44"), "address" : { "building" : "350", "coord" : [ -93.2069217, 43.14769159999999 ], "street" : "5 Avenue", "zipcode" : "10018" }, "name" : "Chipotle Mexican
 Grill", "restaurant_id" : "41223834" }
{ "_id" : ObjectId("5ab23ff163b071e80805df9c"), "address" : { "building" : "350", "coord" : [ -93.2069217, 43.14769159999999 ], "street" : "5 Avenue", "zipcode" : "10018" }, "name" : "Europa Cafe", "r
estaurant_id" : "41229799" }
{ "_id" : ObjectId("5ab23ff163b071e80805e1db"), "address" : { "building" : "350", "coord" : [ -93.2069217, 43.14769159999999 ], "street" : "5 Avenue", "zipcode" : "10018" }, "name" : "Ny Skyride", "re
staurant_id" : "41266025" }
{ "_id" : ObjectId("5ab23ff163b071e80805e3a8"), "address" : { "building" : "1", "coord" : [ -72.4751457, 43.2956803 ], "street" : "Wall Street Court", "zipcode" : "10005" }, "name" : "Haru", "restaura
nt_id" : "41298810" }
{ "_id" : ObjectId("5ab23ff163b071e80805e4ab"), "address" : { "building" : "1618", "coord" : [ -77.6873462, 43.2236789 ], "street" : "Maiden Lane", "zipcode" : "10038" }, "name" : "Oka Sushi", "restau
rant_id" : "41312239" }
{ "_id" : ObjectId("5ab23ff163b071e80805e66b"), "address" : { "building" : "7072", "coord" : [ -85.70723199999999, 42.3625186 ], "street" : "North    6 Street", "zipcode" : "11211" }, "name" : "Black
Bear Bar", "restaurant_id" : "41343482" }
{ "_id" : ObjectId("5ab23ff163b071e80805e6b5"), "address" : { "building" : "350", "coord" : [ -93.2069217, 43.14769159999999 ], "street" : "5 Avenue", "zipcode" : "10018" }, "name" : "Starbucks Coffee
", "restaurant_id" : "41348358" }
{ "_id" : ObjectId("5ab23ff163b071e80805e7d8"), "address" : { "building" : "0", "coord" : [ 0.5595998999999999, 51.3940452 ], "street" : "Pier 78 West 38 Street", "zipcode" : "10018" }, "name" : "Pier
 Side Cafe", "restaurant_id" : "41367417" }
 staurant_id" : "41515837" }
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.