Вопрос

I am trying to get the nearest postings in my meteor project. It is returning my four postings, but not sorted by distance from origin. I added these two indexes:

db.postings.ensureIndex({ loc : "2d" })
db.postings.ensureIndex({ loc : "2dsphere" })

And ran the following query:

Postings.find loc:
    $near:
      $geometry:
        type: "Point"
        coordinates: [ 39.107225, -84.501956 ]

But it returns them in this order:

_id:xs.. 39.239881, -84.359919 15.2 miles (according to google maps)
_id:aK.. 39.239881, -84.359919 15.2 miles
_id:8z.. 39.107225, -84.50195600000001 0 miles
_id:An.. 39.405906, -84.52211699999999 30.5 miles

Here is an example document:

{
"_id": "xsTpnpw7eCAgFS565",
"loc": [39.239881, -84.359919]

}

Это было полезно?

Решение

Try this:

Postings.find loc:
    $near: [ 39.107225, -84.501956 ]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top