1

MongoDB/Railsで地理空間インデックスをクエリする際に問題が発生しました。私はこの宝石を使用しています-https://github.com/kristianmandrup/mongoid_geospatial

これが私のかなり基本的なモデルです:

class Company
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Geospatial

  field :name, type: String

  field :location, type: Array, spatial: true

  spatial_index :location

  validates :location, location: true
end

次に、私のコントローラーには、これがあります

    #@vendors = Vendor.where(:location.near => {:point => [-2.1294761000000335,57.0507625], :max => 5})

ただし、これは期待される結果を返していません(つまり、特定のlon / latの近くだけでなく、あらゆる場所から物事を返しています)

また、これを使ってgeoNearを実行するにはどうすればよいですか?
結果ごとに中心点からの距離を取り戻すことができるようにするには?

この質問を書いた後、宝石が更新されているのを見ましたが、より良い代替案があるかどうかはわかりません。

4

1 に答える 1

4

クエリmongoid_geospatialを実行するのにgemは必要ありません。すでにサポートされています(少なくともバージョン3では)。geoNearmongoid

モデルを次のように変更します。

class Company
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, type: String

  field :location, type: Array

  index({location: "2d"})

  validates :location, location: true
end

そして、次のようにクエリを実行します。

@vendors = Vendor.geo_near([-2.1294761000000335,57.0507625]).max_distance(5)
于 2013-05-26T01:24:46.830 に答える