按距离给定点的距离对位置进行排序,并按照mongoid中的最大距离过滤结果

时间:2014-11-13 20:59:11

标签: ruby-on-rails ruby mongodb mongoid

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

我尝试了以下内容:

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>

1 个答案:

答案 0 :(得分:0)

我得到了它:Mongoid6

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