我有以下架构:
module.exports = function (mongoose) {
var Location = mongoose.Schema({
street: String,
number: String,
zip: String,
floor: String,
rental: String,
size: Number,
loc: {
type: {type: String},
coordinates: []
},
updated: {type: Date, default: Date.now}
});
return mongoose.model('Location', Location);
};
我试图通过以下方式查询数据:
mongoose.models.Location.find({
loc: {
$near: [55.69841959999999,12.5443359],
$maxDistance: 2000
}
}, function (err, doc) {
res.json(doc);
});
但是我得到一个错误说:
> error processing query: ns=RentalDB.locationsTree: GEONEAR field=loc maxdist=2000 isNearSphere=0
Sort: {}
Proj: {}
planner returned error: unable to find index for $geoNear query
谁能告诉我我做错了什么?
答案 0 :(得分:1)
存储时,您的geoData格式应正确。
坐标是数字而不是字符串
参考:https://docs.mongodb.com/manual/geospatial-queries/#legacy-coordinate-pairs
答案 1 :(得分:0)