刚才我开始学习nodejs并正在试验GeoJson数据 . 我试图在传递的坐标100000m内找到所有的忍者 . 对于每个忍者模型,我将其坐标存储在db中 .

但是当我尝试发出get请求时,我收到了这个错误 .

UnhandledPromiseRejectionWarning:MongoError:在C:\ Users \ Administrator的queryCallback(C:\ Users \ Administrator \ Desktop \ NodeJs \ Node Rest \ node_modules \ mongodb-core \ lib \ cursor.js:248:25)中没有geoNear的地理索引\ desktop \ NodeJs \ Node Rest \ node_modules \ mongodb-core \ lib \ connection \ pool.js:532:18 at -combinedTickCallback(internal / process / next_tick.js:132:7)at process._tickCallback(internal / process / next_tick的.js:181:9)

我的代码 -

router.get('/ninjas', function(req, res){

Ninja.aggregate().near({
    near: { type: "Point", coordinates: [parseFloat(req.query.lng),parseFloat(req.query.lat)] },
    maxDistance: 10000,  // in 10k meters
    spherical: true,
    distanceField: "dist.calculated"
  }).then(function(ninjas){
      console.log(ninjas)
    res.send(ninjas)
  });
});

我的架构 -

const GeoSchema= new Schema({
type:{
    type:String,
    default:"Point"
},

coordinates:{
    type:[Number],
    index:"2dSphere"
}
})

const NinjaSchema= new Schema({
name:{type: String,
required:[true,"Name is required"]

},

rank:{
type:String

},
available:{
type:Boolean,
default:false
},

geometry:GeoSchema
});


const Ninja= mongoose.model('ninja', NinjaSchema);

module.exports= Ninja