【发布时间】:2018-06-21 08:50:16
【问题描述】:
我已经尝试寻找解决方案,并且我已经阅读了 $near 和 $geoNear 查询的部分文档,但我没有成功。
var userSchema = new Schema({
zipcode : {
formatted : {type : String, required : false, default : ""},
geo : { type: [Number], default: [0,0]} // long, lat
}
});
userSchema.index({"zipcode.geo" : '2d'});
在不同的文件上
var locationSchema = new Schema({
formatted : {type : String, unique : false, required : true},
geo : { type: [Number], default: [0,0]}
});
locationSchema.index({geo: '2d'});
var storeSchema = new Schema({
locations : [locationSchema],
});
在我的控制器上
let nearQuery = {
$near : [ user.zipcode.geo[0], user.zipcode.geo[1]],
spherical: true,
$maxDistance: 7000
}
Stores.find(locations : nearQuery).exec(function(error, doc) {
});
但我最终得到了错误 - “错误:不能将球形与 Array 一起使用。”
当我删除时
spherical : true
我收到错误 - “规划器返回错误:无法找到 $geoNear 查询的索引”
我知道我做错了什么,我只是不知道如何解决它。我怎样才能解决这个问题?或者这样做的最佳方法是什么?
提前谢谢你。
【问题讨论】:
标签: node.js mongodb mongoose geolocation