MongoDB 'unable to find index for $geoNear query'

前端 未结 4 498
日久生厌
日久生厌 2020-11-29 04:37

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

{\"point\": 
  {\"type\": \"Point\", 
     \"coordinates\"         


        
4条回答
  •  一整个雨季
    2020-11-29 05:08

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

提交回复
热议问题