I am having trouble while executing some spatial queries in MongoDb. I have a collection "cities15000" for which every record has this format
"_id" : ObjectId("5624aefe4728347a51b1d751"),
"geonameid" : "292932",
"name" : "Ajman",
"asciiname" : "Ajman",
"latitude" : "25.41111",
"longitude" : "55.43504",
"feature_code" : "PPLA",
"country_code" : "AE",
"population" : "226172",
"elevation" : "",
"timezone" : "Asia/Dubai",
"geography" : {
"type" : "Point",
"loc" : [
"55.43504",
"25.41111"
]
}
I have created a 2dsphere index db.cities15000.ensureIndex({loc : '2dsphere'})
and then i tried to get results using $near
or $geonear
.
While using $near db.cities15000.find({loc: {$near: [55.400 ,25.400]} })
i get this error message
Unable to execute query: error processing query...n planner returned error: unable to find index for $geoNear query"
which means that i have a wrong index (i think).
But then when i use db.runCommand({geoNear: "cities15000",near: { type: "Point", coordinates: [ 55.400 ,25.400 ] },spherical: true})
i get:
{
"results" : [],
"stats" : {
"nscanned" : 0,
"objectsLoaded" : 0,
"avgDistance" : NaN,
"maxDistance" : 0.0000000000000000,
"time" : 0
},
"ok" : 1.0000000000000000
}
which means that it doesn't find anything near (false). there are many similar topics but i have tried what is proposed there and nothing worked.
you have an error in index creation and the query:
according to your json schema there is no loc but instead there is "geography.loc"
i.e.
db.cities15000.find({loc: {$near: [55.400 ,25.400]} })
should be something like
db.cities15000.find({"geography.loc": {$near: [55.400 ,25.400]} })