ruby-on-rails – Mongoid和查询嵌入式位置?

ruby-on-rails – Mongoid和查询嵌入式位置?,第1张

概述我有一个模型: class City include Mongoid::Document field :name embeds_many :stores index [["stores.location", Mongoid::GEO2D]]endclass Store include Mongoid::Document field :na 我有一个模型:

class City    include MongoID::document    fIEld :name      embeds_many :stores    index [["stores.location",MongoID::GEO2D]]endclass Store    include MongoID::document    fIEld :name    fIEld :location,:type => Array    embedded_in :citIEs,:inverse_of => :storesend

然后我尝试调用类似City.stores.near(@location)的东西.

我想查询City集合以返回在附近位置至少有1个商店的所有城市.我该如何设置索引?什么是最快的电话?

我使用索引[[:location,Mongo :: GEO2D]]阅读了MongoID文档,但我不确定这是如何应用于嵌入式文档,或者如何仅获取City而不是所有Stop文档.

解决方法 麦克风,

您请求的功能称为多位置文档.目前的稳定版本1.8.2不支持它.这仅适用于1.9.1版.

使用mongoID时查询很简单,就像这样

City.near("stores.location" =>  @location)

在多位置文档中使用近查询时要小心,因为同一文档可能会多次返回,因为$near查询会按距离返回有序结果.您可以阅读有关此here的更多信息.

在查询中使用$来获得正确的结果

使用$within和$centerSphere编写的相同查询

EARTH_RADIUS = 6371distance = 5City.where("stores.location" => {"$within" => {"$centerSphere" => [@location,(distance.fdiv EARTH_RADIUS)]}})
总结

以上是内存溢出为你收集整理的ruby-on-rails – Mongoid和查询嵌入式位置?全部内容,希望文章能够帮你解决ruby-on-rails – Mongoid和查询嵌入式位置?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1225761.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存