11

I'm trying to make use of some geolocation functionality in mongodb. Using a find query with $near doesn't seem to work!

I currently have this object in my database:

{
    "Username": "Deano",
    "_id": {
        "$oid": "533f0b722ad3a8d39b6213c3"
    },
    "location": {
        "type": "Point",
        "coordinates": [
            51.50998,
            -0.1337
        ]
    }
}

I have the following index set up as well:

{
  "v": 1,
  "key": {
    "location": "2dsphere"
  },
  "ns": "heroku_app23672911.catchmerequests",
  "name": "location_2dsphere",
  "background": true
}

When I run this query:

db.collectionname.find({ "location" : { $near : [50.0 , -0.1330] , $maxDistance : 10000 }})

I get this error:

error: {
    "$err" : "can't parse query (2dsphere): { $near: [ 50.0, -0.133 ], $maxDistance: 10000.0 }",
    "code" : 16535
}

Does anyone know where I'm going wrong? Any help would be much appreciated!

DeaIss
  • 2,525
  • 7
  • 27
  • 37

2 Answers2

14

It seems you need to use the GeoJSON format if your data is in GeoJSON format too, as yours is. If you use:

db.collectionname.find({
    "location": {
        $near: {
            $geometry:
                { type: "Point", coordinates: [50.0, -0.1330] }, $maxDistance: 500
        }
    }
})

it should work. I could replicate your error using GeoJSON storage format for the field, but what the docs call legacy points in the query expression. I think the docs are a bit unclear in that they suggest you can use both GeoJSON and legacy coordinates with a 2dsphere index 2dsphere

I am using 2.4.10, for what it is worth, as there were some big changes to spatial in the 2.4 release.

Liyang
  • 75
  • 2
  • 10
John Powell
  • 12,253
  • 6
  • 59
  • 67
  • Hey, I'm actually trying to search the first nearest location regardless maxDistance. It works but it takes 4 seconds, it's really slow. There is a way to increase the performance ? – John Dec 16 '16 at 09:59
  • @John. I don't know from the information you have provided, but leaving out maxDistance means search the whole db, which depending on the the size, could take a very long time. – John Powell Dec 16 '16 at 10:13
  • `$nearSphere` returns from nearest to farthest right ? Then I'm using limit(). My location in my database looks like this : `[-5.95001220703125,46.149993896484375]` so when I send `[-5, -46]` this will take 2 - 3 seconds. But if I send this location E.g `[-5.953801, 46.001843]` this is really fast. I get the result just in `22ms`. So it's normal ? – John Dec 16 '16 at 10:33
1

This isn't exactly a solution as I never got the above working, but using geoNear I managed to get what I wanted.

db.runCommand( { geoNear : 'catchmerequests', near: 
{ type: 'Point', coordinates : [50, 50] }, spherical : true } );

If anyone can find out why the original $near attempt failed that would still be appreciated, but I'm posting this for anyone else who else who is looking for a working alternative.

DeaIss
  • 2,525
  • 7
  • 27
  • 37
  • I have posted what I think is a solution. Have been meaning to play with mongo spatial for a while -- I'm a Postgis guy -- and I enjoyed that. Thanks. – John Powell Apr 05 '14 at 20:59
  • @Dealss I noticed the above answer was marked as being the solution but I see here that it didn't work for you and you are using the geoNear solution. I saw the below reference in the MongoDB docs and was wondering if you did the same thing as me and "inserted" your document. _For inserts, MongoDB inserts the document but does not add to the 2dsphere index._ Also did you get the original solution to work with $near? http://docs.mongodb.org/manual/core/2dsphere/ – Ravenous Sep 28 '15 at 14:15