MongoError:找不到任何特殊的索引:2d(需要索引),2dsphere(需要索引)$ nearSphere

这是我的第一个问题,所以如果问得不好,或者我的英文不好,请耐心等待。

我正在尝试在Node.js中在MongoDB中执行Geospatioal Querys

这个查询工作得很好

collection.find( { loc : { $geoWithin : { $centerSphere : [ [ point.coordinates[0], point.coordinates[1]] , getRadiusMeters(radius) ] } } } ) 

但是当我试图用$ nearSphere做同样的事情时:

 collection.find( { loc :{ $nearSphere : { $geometry : { type : "Point" , coordinates : [ point.coordinates[0], point.coordinates[1]] } , $maxDistance : getRadiusMeters(radius) } } } ) 

我得到以下错误:

 { [MongoError: can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { loc: { $nearSphere: { $geometry: { type: "Point", coordinates: [ 6.128777, 49.596792 ] }, $maxDistance: 10000 } } }] name: 'MongoError' } 

我想我有正确的索引。 这里是我的索引:

 > db.realtrip.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.realtrip", "name" : "_id_" }, { "v" : 1, "key" : { "loc" : "2dsphere" }, "ns" : "test.realtrip", "name" : "loc_2dsphere" }, { "v" : 1, "key" : { "tags" : 1 }, "ns" : "test.realtrip", "name" : "tags_1" }, { "v" : 1, "key" : { "loc" : "2d" }, "ns" : "test.realtrip", "name" : "loc_2d" } ] 

我无法find我做错了什么。 $ centerSphere Query工作得很好,只有$ nearSphere Query给我提供了错误。

事实certificate,我的错误是有关这个问题Bugreport Mongodb

更新我的mongodb解决了它。 现在代码按预期工作。