MongoDB 'unable to find index for $geoNear query'



我只是想让一个简单的near查询正常工作。这是我的文件样本。

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

我用mongod尝试了命令:

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

但我得到了这个错误:

错误:{"$err":"无法执行查询:处理查询时出错:ns=foo.bar skip=0\n查询:GEONEAR字段=点maxdist=1.79769e+308 isNearSphere=0||第一个:notFirst:完整路径:点\n第二个:{}\nJ:{}\n计划器返回错误:无法找到$GEONEAR查询的索引","代码":17007}

也许我今天的谷歌功夫不太好,但我什么也找不到。此外,我还运行了确保索引命令。我的意图是,这些是地图位置。

db.bar.ensureIndex({a:1});
db.bar.ensureIndex({geo:"2d"});

很少有问题,您在foo数据库的foo集合上创建了索引,但正在查询bar集合。你需要在正确的集合中。

阅读您插入的文档时,需要添加一个"2dsphere"索引来支持geoJson对象。这个索引需要在文档的"点"元素上,所以请尝试

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

然后,您可以通过为查询提供geoJson obj来进行如下查询:

db.bar.find(
   { point :
       { $near :
          {
            $geometry : {
               type : "Point" ,
               coordinates : [-84.27326978424058, 30.443902444762696] },
            $maxDistance : 1
          }
       }
    }
)
db.prod.createIndex({ "location": "2d" })

这为我解决了同样的问题。

其中prod是我的集合名称,location是存储地理位置(GeoPoint)的列的名称

关于这一点的一些讨论可以在这里找到

所以这里似乎有几个问题:

  • 根据您显示的数据以及查询信息,相关信息包含在字段点下,并采用GeoJSON格式。您的索引创建:
db.foo.createIndex({geo:"2d"})

不会"失败",因为目前没有一个名为"geo"的字段,并且包含数据的字段应该在那个位置。如果您使用了"点",这是正确的字段,那么您会收到一个错误,告诉您这种类型的索引对GeoJSON数据无效。你需要一个"2dsphere"索引:

db.points.createIndex({ "point": "2dsphere" })
  • 扩展了同样的问题,数据也是GeoJSON格式的,查询的形式是传统坐标对的形式。您需要更改查询参数,使其不再失败:
db.points.find({point:{$near:{$geometry:{类型:"点",坐标:[84.26060492426588,30.45023887165371]}}}})

请参阅$near

的文档

除了上面的答案之外,如果您已经尝试创建索引,但出现了一些语法或字段错误,则可以运行

db.<yourcollection>.dropIndexes();清理所有索引并正确地重新创建它们。

此外,索引应该在"坐标"的父级上创建,而不是在坐标本身上:

{
   "_id": 59ac03d168eaaa14c2a57a00",
   "location":{
      "type":"Point",
      "coordinates":[
         131.6667,
         57.8368
      ]
   },
   "age":53,
   "username":"Brandi_Greenfelder"
}

db.<yourcollection>.createIndex({ location: '2dsphere' });

注意,有"2d"one_answers"2dsphere",使用第二个,因为它是新事物。

如果您使用猫鼬进行连接,这将是正确的答案:

db.collections.<yourcollection>.createIndex({ location : "2dsphere" })

注意有一个";集合";属性在集合本身之前。如果不起作用,请检查console.log中的db对象:

console.log(db)

如果有人正在从spring boot starter数据mongodb 2迁移到3

默认情况下,他们取消激活索引的自动配置,这可能会导致'unable to find index for $geoNear query' 出现此错误

@Configuration
public class Config extends AbstractMongoClientConfiguration {
    @Override
    protected boolean autoIndexCreation() {
        return true;
    }
    // ...
}

在我的例子中,我拥有了所有内容(有效的GeoJSON,2dsphere索引),但错误仍然出现。问题是,在创建/保存新对象时,我未能在Schema定义中为index指定相同的2dsphere值:

// Geo index for new object
const boxSchema = new Schema<IBoxRecord>({
  id: { type: Number, required: true },
  name: { type: String, index: true, required: true },
  location: { 
    type: Object,
    index: "2dsphere", // <--- previously set to `true` by mistake
    require: true 
  }
});
...
// index existing collection
await BoxRecord.collection.createIndex({ location: "2dsphere" });

最新更新