【问题标题】:struggling with geospatial queries on mongo在 mongo 上苦苦挣扎的地理空间查询
【发布时间】:2022-01-17 07:24:12
【问题描述】:

几天来,我一直在努力让查询正常工作。目前我的模型如下所示:

class Geojson
  include Mongoid::Document

  field :type, type: String, default: 'Point'
  field :coordinates, type: Array

  index({coordinates: "2dsphere"}, { bits: 12}) 
end

以下查询返回 nil:

Geojson.find(:coordinates => {"$nearSphere" => [-70.1197340629727, 4.67071244438]})

这些是我数据库中的当前实例:

[#<Geojson _id: 61b7b21a9eb0c9ef0aa5626d, type: "Point", coordinates: [-74.13041168951031, 4.6638117]>,
#<Geojson _id: 61b7b2619eb0c9ef0aa5626e, type: "Point", coordinates: [-74.1213041168951, 4.5638117]>] 

我可以在 mongosh 上毫无问题地查询类似的案例,但是我不确定直接在 rails 上做的错误在哪里。

【问题讨论】:

    标签: ruby-on-rails ruby mongodb geojson mongo-shell


    【解决方案1】:

    我终于设法通过以下方式使其工作:(对于 2d 球体索引)

    Geojson.where(:coordinates => {"$nearSphere" => [long, lat]}).to_a
    

    其中经度和纬度是接收到的参数。

    【讨论】:

      猜你喜欢
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 2013-02-20
      • 2020-08-06
      • 1970-01-01
      • 2019-01-20
      相关资源
      最近更新 更多