Node.js mongodb-使用$geoNear提供;geo-near在查询GeoJSON点时只接受一个参数;使用maxDistance时
这似乎是一个常见的错误,但我似乎无法使它与我所看到的所有建议一起工作。这是我的设置:Node.js mongodb-使用$geoNear提供;geo-near在查询GeoJSON点时只接受一个参数;使用maxDistance时,node.js,mongodb,mongoose,geojson,geonear,Node.js,Mongodb,Mongoose,Geojson,Geonear,这似乎是一个常见的错误,但我似乎无法使它与我所看到的所有建议一起工作。这是我的设置: // 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 pointSchem
// 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;
我得到了这个错误:
MongoError:无法确定查询系统是否可以提供覆盖投影::由::geo near引起
查询GeoJSON点时只接受一个参数。找到额外字段:$maxDistance:100000.0
我看到的大多数线程都表明这是索引问题,但您可以在user.js
中看到我直接调用的
schema.index({ location: '2dsphere' });
没有任何运气
我真的很感激任何建议,谢谢 我也遇到了同样的问题,并经历了许多解决方案,但其中任何一个都没有达到预期效果 真正的问题在于数据 在我的模型中,2dsphere索引是正确的,但当使用2dshpere时,数据库中的位置应采用location:[lng,lat]格式,这是主要问题。因为mongodb不验证此格式的位置数组 验证数据库中的数据和查询时指定的点的格式是否正确
希望它能帮助别人 没错。在我的例子中,问题是我将坐标作为字符串而不是数字提供。
// 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);
});
schema.index({ location: '2dsphere' });