1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.