Ishpreet
Ishpreet

Reputation: 5880

What's the correct syntax for given mongodb query?

Question: Write a MongoDB query to find the restaurants which locates in latitude value less than -95.754168.

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"
}

Author's Answer is : db.restaurants.find({"address.coord" : {$lt : -95.754168}});

Is this the correct answer? If no then what is the correct answer?

Resource: http://www.w3resource.com/mongodb-exercises/#PracticeOnline

Upvotes: 1

Views: 157

Answers (1)

Zhi Kai
Zhi Kai

Reputation: 1587

You can go into your coord index like this

db.restaurants.find({"address.coord.0" : {$lt: -95.754168}});

According to the documentation here,

MongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document.

E.g. <array>.<index>

Note that the index is 0-based.

Upvotes: 1

Related Questions