2012-03-09 30 views
0

我有一個翻譯文檔嵌入許多翻譯的語言環境文件:的MongoDB:對多個按鍵的搜索嵌入文檔

class Translation 
    include Mongoid::Document 

    field :key, :type => String 
    embeds_many :locales, :class_name => 'TranslationLocale' 
end 

class TranslationLocale 
    include Mongoid::Document 

    embedded_in :translation 

    field :code, :type => String 
    field :state, :type => Boolean, :default => false 
    field :text, :type => String 
end 

我希望能夠找到所有翻譯文件,包括在特定狀態的特定區域。

Translation.where('locales.code' => 'en', 'locales.state' => false).all 

的問題是,查詢將尋找翻譯文檔中嵌入的代碼= EN和狀態= false一個區域一個區域,而並不一定相同的子文件。

任何幫助表示讚賞,謝謝!

回答

1

試試這個:

Translation.where(:locales.matches => {:code=> 'en', :state=> false}).all 

here

+0

你的岩石的例子!我不知道我在文檔中錯過了這些。我曾嘗試類似的語法,但沒有「匹配」關鍵字... – Kochka 2012-03-09 14:24:40