0

When I make a query to my MongoDB database using mongoose and the mongoDB $near geospatial operator (also tried $nearSphere) my code returns a 200 HTTP status with the correct list of documents but they are not sorted from nearest to furthest (which unfortunately defeats the purpose of the $near operator).

I made sure that I have a "2dsphere" index created on my geometry key and that this key is formatted according to the GeoJson guidelines.

This is the code I use to query the database:

const locations = await Location.find({
        geometry: {
            $near: {
                $geometry: {
                    type: 'Point',
                    coordinates: [lon, lat]
                }
            }
        }
    });

Where lat and lon are Floats.

The 3 first objects returned from my app are the following:

{
    "_id": "5e2ef89e326e885327c6e898",
    "address": "20 Brune Street",
    "availability": false,
    "distance_to": 10583,
    "power": 3840,
    "coordinates": {
      "latitude": 51.51792,
      "longitude": -0.075343
    }
  },
  {
    "_id": "5e2ef79d326e885327c6e882",
    "address": "32 Bedford Row",
    "availability": true,
    "distance_to": 8028,
    "power": 3840,
    "coordinates": {
      "latitude": 51.520297,
      "longitude": -0.116327
    }
  },
  {
    "_id": "5e2ef801326e885327c6e891",
    "address": "42 York Way",
    "availability": false,
    "distance_to": 8322,
    "power": 3840,
    "coordinates": {
      "latitude": 51.532265,
      "longitude": -0.12251
    }
  }

Please note that I deliberately do not include the GeoJson key in the returned objects but they are present in each document of the Database.

Am I missing something?

Thanks in advance!

eliotmkb
  • 1
  • 2
  • 1
    The documentation indicates that $near does indeed sort the documents. You mention that your code returns 200OK, which indicates, that you have some sort of application in between. Couldn't that be modifying your results? Can you log what query returns? Doc: https://docs.mongodb.com/manual/reference/operator/query/near/ – Tom Apr 13 '20 at 21:30
  • Hi Tom, thanks for your response. I am indeed looking at the results of my API endpoint but to make sure that I wasn't modifying the order I have tried to also look at the raw data coming back from the database without any other selection queries. This was also sorted in the same ineaxct way. The original objects coming from the DB are quite large so I've just put the 3 first objects from the response, along with a "distance_to" key that represents the distance to that location from the quiried coordinates. – eliotmkb Apr 13 '20 at 23:16
  • I haven't used mongodb in a while, but from looking the documentation it seems that you are querying against the wrong key. You are querying against `geometry`, but the coordinates in your documents are stored in `coordinates`. Have a look at how the document and query is modeled here: https://stackoverflow.com/questions/22881401/mongodb-find-query-with-near-and-coordinates-not-working – Tom Apr 14 '20 at 06:46
  • The `geometry` key doesn't appear in my response because these objects were formatted and slightly changed (but **NOT** the order) by the API before being sent back. In my database, each object has a `geometry` key that is properly formatted. – eliotmkb Apr 14 '20 at 08:22
  • All fine now, found my error! In my database the `geometry` Object's `coordinates` array had latitude first instead of longitude first. I guess my objects weren't "formatted according to the GeoJson guidelines" like I said, my bad. – eliotmkb Apr 14 '20 at 20:06

0 Answers0