Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB 'unable to find index for $geoNear query'

I'm just trying to get a simple near query working. Here's a sample of my document.

{"point": 
  {"type": "Point", 
     "coordinates": [30.443902444762696, -84.27326978424058]}, 
   "created_on": {"$date": 1398016710168}, 
   "radius": 180, 
   "user": {"$oid": "53543188eebc5c0cc416b77c"}, 
   "_id": {"$oid": "53544306eebc5c0ecac6cfba"}, 
   "expires_on": {"$date": 1399831110168}
}

and with mongod I tried the command:

db.bar.find({point: {$near: [-84.26060492426588, 30.45023887165371]}});

but I get this error:

error: { "$err" : "Unable to execute query: error processing query: ns=foo.bar skip=0\nTree: GEONEAR field=point maxdist=1.79769e+308 isNearSphere=0 || First: notFirst: full path: point\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query", "code" : 17007 }

Maybe my google fu is not so sharp today but I couldn't find anything. Also, I ran the ensure index command. My intention is that these are map locations.

db.bar.ensureIndex({a:1});
db.bar.ensureIndex({geo:"2d"});
like image 595
frankV Avatar asked Apr 20 '14 23:04

frankV


3 Answers

Few problems, you created your indexes on the foo collection of the foo database, but are querying the bar collection. You need to be on the correct collection.

Reading the document you have inserted you need to add a "2dsphere" index to support the geoJson objects. This index needs to be on the "point" element of your documents, so try

db.bar.createIndex({point:"2dsphere"});

You can then query as follows by providing a geoJson obj for the query:

db.bar.find(
   { point :
       { $near :
          {
            $geometry : {
               type : "Point" ,
               coordinates : [-84.27326978424058, 30.443902444762696] },
            $maxDistance : 1
          }
       }
    }
)
like image 172
daveh Avatar answered Nov 08 '22 17:11

daveh


db.prod.createIndex({ "location": "2d" })

This solved for the same issue for me.

Where prod is my collection name and location is name of column which stores geo location (GeoPoint)

Some discussion about the same can be found here

like image 40
Krish Nakum R Avatar answered Nov 08 '22 17:11

Krish Nakum R


So there seems to be a couple of things wrong here:

  • From the data you are showing and also your query information the relevant information is contained under the field point and in GeoJSON format. Your index creation:
db.foo.createIndex({geo: "2d"})

Does not "fail" because there presently isn't a field called "geo" and the field with the data should have been in that place. If you had used "point" instead, which is the correct field, then you would have received an error telling you that this type of index is invalid for the GeoJSON data. You need a "2dsphere" index:

db.points.createIndex({ "point": "2dsphere" })
  • Extending the same problem, again the data is in GeoJSON format and the form of the query is that for a legacy coordinate pair. You need to change the query arguments so that no longer fails:
db.points.find({point: {
    $near: {
        $geometry:{ 
            type: "Point", 
            coordinates: [-84.26060492426588, 30.45023887165371]
        }
    }
}})

See the documentation for $near

like image 7
Neil Lunn Avatar answered Nov 08 '22 18:11

Neil Lunn