MongoDB:在嵌入文档时搜索多个键

时间:2012-03-09 12:18:41

标签: ruby-on-rails mongodb mongoid

我有一个翻译文档嵌入了许多翻译区域设置文档:

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

问题是查询将查找嵌入了具有code = en的语言环境的翻译文档和具有state = false但不一定在同一子文档上的语言环境。

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

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

来自here

的示例
相关问题