MongoDB ‘unable to find index for $geoNear query’

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
          }
       }
    }
)

Leave a Comment