Sid
Sid

Reputation: 2156

Geospatial index in Mongo created but errors when used

I have a collection called geonames structured as follows:

{
    ...
    loc: [50.729400634765625, 15.723899841308594]
},
...

I then create an index as follows:

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

I ensure the index was correctly created:

> db.geonames.getIndexes()          
[
        {
                "name" : "_id_",
                "ns" : "mydb.geonames",
                "key" : {
                        "_id" : 1
                }
        },
        {
                "_id" : ObjectId("4f033a6ff50d07e36849464b"),
                "ns" : "mydb.geonames",
                "key" : {
                        "loc" : "2d"
                },
                "name" : "loc_"
        }
]

However when I try to query using this:

> db.geonames.find({$near: [50,15]})
error: {
        "$err" : "can't find special index: 2d for: { $near: [ 50.0, 15.0 ] }",
        "code" : 13038
}

What am I doing wrong?

Upvotes: 0

Views: 1796

Answers (1)

Nat
Nat

Reputation: 3727

instead

db.geonames.find({$near: [50,15]})

try

db.geonames.find({loc : {$near: [50,15]}})

Upvotes: 4

Related Questions