如果存储点的模式中存在多个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的文档说明如下。
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});
});因此,我的问题是,如何才能获得每个存储点的距离,从输入的感兴趣点使用上面描述的模式。
任何帮助都是很好的,因为我的互联网搜索不会去任何地方。
谢谢你,夫人
发布于 2016-12-05 23:56:37
正如您注意到的,mongodb文档声明
geoNear命令和$geoNear管道阶段要求集合最多只有一个2dsphere索引和/或一个2d索引。
另一方面,计算mongo内部的距离只有在聚合框架中才能实现,因为它是一个专门的投影。如果你不想选择
那么你的另一个选择是
https://stackoverflow.com/questions/37565251
复制相似问题