【问题标题】:Sorting locations by distance from a given point and filtering the results by a maximal distance in mongoid按距给定点的距离对位置进行排序,并按 mongoid 中的最大距离过滤结果
【发布时间】:2014-11-13 20:59:10
【问题描述】:

我有一个集合,其中每个文档都有一个位置。我需要按文档到给定起点的距离对文档进行排序。此外,我只想拥有与起点的距离不超过一定限制的文档。

我尝试了以下方法:

Store.geo_near([32,32]).where(:geo_near_distance.lt => 2) 
# all docs whose location are at most 2 from [32, 32]

以上导致如下错误:

NoMethodError: undefined method `where' for #<Mongoid::Contextual::GeoNear:0x438fb70>

【问题讨论】:

    标签: ruby-on-rails ruby mongodb mongoid


    【解决方案1】:

    我得到了它的工作:Mongoid6

      Store.unscoped.where(
         :coordinates => {
           '$nearSphere' => your_store.coordinates,
           '$maxDistance' => distance_in_km.fdiv(111.12)})
    

    【讨论】: