簡體   English   中英

Mongoid 和查詢嵌入式位置?

[英]Mongoid and querying for embedded locations?

我有一個 model 沿線:

class City
    include Mongoid::Document
    field :name  
    embeds_many :stores

    index [["stores.location", Mongoid::GEO2D]]
end

class Store
    include Mongoid::Document
    field :name
    field :location, :type => Array
    embedded_in :cities, :inverse_of => :stores
end

然后我試着打電話給City.stores.near(@location)

我想查詢City集合以返回在附近位置至少有 1 Store的所有城市。 我應該如何設置索引? 最快的電話是什么?

我使用index [[:location, Mongo::GEO2D]]閱讀了 Mongoid 文檔,但我不確定這如何應用於嵌入式文檔,或者如何僅獲取City而不是所有Stop文檔。

麥克風,

您請求的功能稱為多位置文檔。 當前的穩定版本 1.8.2 不支持它。 這僅在版本 1.9.1 中可用。

並且使用mongoid時查詢很簡單,就像這樣

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

在多位置文檔中使用近距離查詢時要小心,因為可能會多次返回同一個文檔,因為 $near 查詢會返回按距離排序的結果。 您可以在此處閱讀有關此內容的更多信息。

改用 $within 查詢來獲得正確的結果

使用 $within 和 $centerSphere 編寫的相同查詢

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM