Mongodb Geonear和多个2dsphere索引

Mongodb Geonear和多个2dsphere索引,mongodb,autocomplete,maps,latitude-longitude,Mongodb,Autocomplete,Maps,Latitude Longitude,如果存储点的模式中存在多个2dsphere索引,我有一个关于使用$near vs geonear返回从用户输入的兴趣点到数据库中存储点的距离的问题 用例如下所示 在我的模式中,我有一个源位置和一个目标位置,如下所示。使用Intracity.find的查询工作正常,并从输入的兴趣点为我提供已排序的条目 var baseShippingSchema = new mongoose.Schema({ startDate : Date, endDate : Date, locS

如果存储点的模式中存在多个2dsphere索引,我有一个关于使用$near vs 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是不可能的,但可以使用geonearapi

然而,geonear的首席执行官说:

geoNear需要地理空间索引。但是,geoNear命令要求集合最多只有一个二维索引和/或一个二维球体。

由于在我的模式中,我有两个2dspehere索引,以下geonear api失败,错误为“多个2d索引,不确定在哪个上运行geonear

因此,我的问题是,如何使用上述模式获得每个存储点与输入的兴趣点之间的距离

任何帮助都是非常好的,因为我的互联网搜索没有任何进展

多谢各位 Mrunal

如您所述

geoNear命令和$geoNear pipeline stage要求集合最多只有一个2dsphere索引和/或一个2d索引

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

  • 关系数据库方法:在所有项之间维护一个单独的距离表
  • 那么你的另一个选择就是

  • 文档存储方法:在服务器端JS代码中计算距离。您必须通过分页结果来覆盖内存限制
  • 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});
            });