按距给定点的距离对位置进行排序,并按最大距离(以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>

我通过以下方式工作:Mongoid6

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