0

I am trying to put location data from my android application to mongo db database. Data is getting inserted into the database. But, when retrieving I am getting an error saying:

" "$err" : "Unable to execute query: error processing query: ns=demo.search limit=0 skip=0\nTree: GEONEAR  field=loc maxdist=1 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query",
...         "code" : 17007
"

I have added loc index as 2dsphere with createIndex command. What is actually wrong with my query?

My query:

db.collection('search').find(

                  {"loc":
                        { $near :coords ,$maxDistance: maxDistance}
                  }
                );
halfer
  • 19,824
  • 17
  • 99
  • 186
User007
  • 56
  • 3
  • In the shell `db.getCollection('search').getIndexes()` Show the output in your question. Show a document from the collection in your question. You can tell us what you have as much as you like, but the error says otherwise. Show the details asked for and then we can tell you what you are doing incorrectly. – Neil Lunn Aug 11 '17 at 10:35
  • [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "demo.search" }, { "v" : 1, "key" : { "loc" : "2dsphere" }, "name" : "loc", "ns" : "demo.search", "2dsphereIndexVersion" : 2 } ] – User007 Aug 11 '17 at 10:39
  • @NeilLunn this is what I am getting in the shell – User007 Aug 11 '17 at 10:39
  • duplicate of https://stackoverflow.com/questions/23188875/mongodb-unable-to-find-index-for-geonear-query – Eric Aug 11 '17 at 15:08
  • Possible duplicate of [MongoDB 'unable to find index for $geoNear query'](https://stackoverflow.com/questions/23188875/mongodb-unable-to-find-index-for-geonear-query) – Eric Aug 11 '17 at 15:09

1 Answers1

0

Got it resolved by creating an index with 2d. Initially I tried 2dsphere that was the reason my code was not working.

User007
  • 56
  • 3