我想查询Mongoid类的哈希字段。我不确定在有条件的情况下怎么办?
下面是一个例子:
class Person include Mongoid::Document field :things, :type => Hash end
所以,假设我有这个:
p = Person.new p.things = {} p.things[:tv] = "Samsung"
我想询问第一个拥有三星电视的人。。。
People.first(:conditions => ?????
提前谢谢。
Person.where('things.tv' => 'Samsung').first
这就是Mongoid和MongoDB真正的亮点所在。Mongoid准则方法( Person.where , Person.any_of , Person.excludes 等等)会给你比ActureCordD风格发现者更大的灵活性(通过A) :conditions 散列到 Person.find , Person.first 等)
Person.where
Person.any_of
Person.excludes
:conditions
Person.find
Person.first
Mongoid的站点有一些关于如何使用 Criteria :
Criteria
http://mongoid.org/en/mongoid/docs/querying.html