我在使用MongoDB Rails查询地理空间索引时遇到了问题。https:/github.comkristianmandrupmongoid_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?这样我就可以得到每个结果的中心点的距离?
注写完这个问题,我看到创业板已经更新了,但不知道有没有更好的选择...?
你不需要 mongoid_geospatial
创业 geoNear
查询。mongoid
已经支持了(至少在第三版)。
把你的模型改成。
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)