I’m struggling to figure out how to search a collection and display results by proximity using $near. In my collection schema I have defined an array containing my longitude and latitude, like so:
In the template where I want to display my results, I’m using the following in my helper:
return Therapists.find({
geopoint: {
$near: [Session.get('userLng'),Session.get('userLat')], //these values are correctly set
$maxDistance: 2000
}
});
This seems to work in principle, but it returns ALL the documents, even those where the two coordinates stored in geopoint are on the other side of the world and regardless of the $maxDistance value. This also happens if I replace the session variables with static values. Am I doing this wrong?
Thanks none. The problem is that when I add this to my schema (correcting the typo in location.coordinates) I get the following error: “MongoError: can’t parse geometry from element:location.”
I’m wondering whether this error maybe has something to do with the fact that I’m using simpleSchema and my schema is being used with autoform? Any thoughts?
Okay, this is really strange…It looks like I get the error if I add your example to my schema OR if I just add: Therapists._ensureIndex({location:"2dsphere"});
So even if I don’t use your schema I’m still getting the error as soon as I add the _ensureIndex…
Oooh! Wait! It looks like I have it working. I think the issue was that I was using deprecated syntax. I’ve replaced my helper with the following and it works