2

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]

}

timmyg13
  • 503
  • 1
  • 5
  • 15

1 Answers1

0

Try this:

Postings.find loc:
    $near: [ 39.107225, -84.501956 ]
  • i was using the 2dsphere index and a query which passed the GeoJSON object (with type & coordinates properties), my sorting did not work. it also did not work in the mongo command line. then i changed to this style of query and a plain-old 2d index, everything worked. what gives? – Randy L Sep 25 '14 at 19:24