【问题标题】:Geonear and more than one 2dsphere indexesGeonear 和多个 2dsphere 索引
【发布时间】: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


    【解决方案1】:

    正如您所说的 mongodb docs 状态

    geoNear 命令和 $geoNear 管道阶段要求集合最多只有一个 2dsphere 索引和/或只有一个 2d 索引

    另一方面,在 mongo 中计算距离只能使用聚合框架,因为它是一个专门的投影。如果你不想选择

    1. 关系数据库方法:在所有项目之间维护一个单独的距离表

    那么你的另一个选择是

    1. 文档存储方法:在您的服务器端 JS 代码中计算距离。您必须通过对结果进行分页来覆盖内存限制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-24
      • 2013-07-03
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 2019-09-16
      相关资源
      最近更新 更多