这似乎是一个常见错误,但我似乎无法让它与我看到的所有建议一起工作。这是我的设置:
// point.js (based on mongoose recommended subdocument pattern for reusing the GeoJSON definition
// see here https://mongoosejs.com/docs/geojson.html)
const mongoose = require('mongoose');
const pointSchema = new mongoose.Schema({
type: {
type: String,
enum: ['Point'],
required: true
},
coordinates: {
type: [Number],
required: true,
}
});
exports = pointSchema;
// user.js
var schema = new Schema({
location: {
type: pointSchema,
},
...
});
schema.index({ location: '2dsphere' });
var User = mongoose.model('User', schema);
// routeHandler.js
const near = { type: 'Point', coordinates: [lng, lat] };
User.aggregate([
{ $geoNear: {
near,
distanceField: 'dist',
maxDistance: 100000,
spherical: true,
} },
...
]).exec((err, results) => {
console.log('error or results:', err, results);
});
我得到这个错误:
MongoError:无法确定查询系统是否可以提供由 :: geo 引起的覆盖投影 :: 在查询 GeoJSON 点时仅接受一个参数。发现额外字段:$maxDistance:100000.0
我见过的大多数线程都表明这是索引的问题,但您可以看到user.js我直接调用
schema.index({ location: '2dsphere' });
没有任何运气。
我真的很感激任何建议,谢谢!