【发布时间】:2018-07-11 15:45:05
【问题描述】:
我正在尝试使用 mongodb 获取基于纬度和经度的记录。我经历了很多例子,但对我没有任何帮助。
这是我的架构结构。
latlang: {
type: { type: String, default: 'Point'},
coordinates: [Number]
}
存储值后的样子
latlang:{
type: "Point",
coordinates: [77.6836, 12.8486]
}
我正在为 latlang 字段设置索引,例如
schema.index({ 'latlang.coordinates' : '2dsphere' });
我有多个记录,如下所示
latlang:{
type: "Point",
coordinates: [77.6836, 12.8486]
}
latlang:{
type: "Point",
coordinates: [77.5712, 12.9766]
}
latlang:{
type: "Point",
coordinates: [77.6174, 12.9226]
}
当我尝试使用以下查询获取记录时,我得到空响应
User.find({ loc:{ $near:{ $geometry: {type: "Point" , coordinates:[77.6974,12.9591] }, $maxDistance:10000 }}}).exec();
任何帮助将不胜感激。提前致谢!
【问题讨论】:
-
您定义了一个
latlang索引和架构,但在您的查询中,您正在过滤一个loc字段。我认为这可能是一个错字。
标签: mongodb