Cathal Coffey
Cathal Coffey

Reputation: 1127

$near requires geojson point, given geojson point

I have documents which contain a geo field like the below.

"geo" : {
    "type" : "Point",
    "coordinates" : [
        37.44609999,
        -121.88355687
    ]
},

I have created a spatial index, proof below.

db.collection.getIndexes()
[
    {
        "v" : 1,
        "key" : {
        "_id" : 1
        },
        "ns" : "db.collection",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
        "geo.coordinates" : "2dsphere"
        },
        "ns" : "db.collection",
        "name" : "geo.coordinates_2dsphere"
    }
]

However when I execute the following query (verbatim from the MongoDB manual ).

db.collection.find( { 'geo.coordinates': { $near: 
                                            { $geometry :
                                                { type : "Point", 
                                                  coordinates: [ 37.44609999, -121.88355687 ] } }, 
                                                  $maxDistance: 1000
                                         } } )

I get the following error.

error: {
    "$err" : "$near requires geojson point, given { type: \"Point\", coordinates: [ 37.44609999, -121.88355687 ] }",
    "code" : 16681
}

Can someone enlighten me as to why?

Upvotes: 3

Views: 2278

Answers (1)

astone26
astone26

Reputation: 1232

Flip the points as such:

"geo" : { "type" : "Point", "coordinates" : [ -121.88355687, 37.44609999 ] },

Otherwise, check your coordinates to see if they are real: http://geojsonlint.com/

Upvotes: 2

Related Questions