【发布时间】:2016-09-30 14:53:08
【问题描述】:
如果存储点的模式中存在多个 2dsphere 索引,我有一个关于使用 $near 与 geonear 从用户输入的兴趣点返回数据库中存储点的距离的问题。
用例如下。
在我的架构中,我有一个源位置和一个目标位置,如下所示。使用 Intracity.find 的查询正常工作,并为我提供了从输入的兴趣点排序的条目。
var baseShippingSchema = new mongoose.Schema({
startDate : Date,
endDate : Date,
locSource: {
type: [Number],
index: '2dsphere'
},
locDest: {
type: [Number],
index: '2dsphere'
}
});
var search_begin = moment(request.body.startDate0, "DD-MM-YYYY").toDate();
var search_end = moment(request.body.endDate1, "DD-MM-YYYY").toDate();
var radius = 7000;
Intracity.find({
locSource: {
$near:{$geometry: {type: "Point",
coordinates: [request.body.lng0,request.body.lat0]},
$minDistance: 0,
$maxDistance: radius
}
}).where('startDate').gte(search_begin)
.where('endDate').lte(search_end)
.limit(limit).exec(function(err, results)
{
response.render('test.html', {results : results, error: error});
}
但是,我还想返回存储点与兴趣点的“距离”,根据我的知识和发现,使用 $near 是不可能的,但可以使用 geonear api。
但是,geonear 的documentation 表示以下内容。
geoNear 需要地理空间索引。但是,geoNear 命令要求一个集合最多只有一个 2d 索引和/或只有一个 2dsphere。
由于在我的架构中我有两个 2dspehere 索引,因此以下 geonear api 失败并出现错误“多个 2d 索引,不确定要在哪个上运行 geoNear”
var point = { name: 'locSource', type : "Point",
coordinates : [request.body.lng0 , request.body.lat0] };
Intracity.geoNear(point, { limit: 10, spherical: true, maxDistance:radius, startDate:{ $gte: search_begin}, endDate:{ $lte:search_end}}, function(err, results, stats) {
if (err){return done(err);}
response.render('test.html', {results : results, error: error});
});
所以我的问题是如何使用上述模式从输入的兴趣点获取这些存储点中的每一个的距离。
任何帮助都会非常棒,因为我的 Internet 搜索无处可去。
谢谢
【问题讨论】:
标签: mongodb autocomplete maps latitude-longitude