查询 Mongoid 哈希字段 [英] Query on Mongoid Hash Field
问题描述
我想查询 Mongoid 类的哈希字段.我不确定如何在有条件的情况下做到这一点?
I want to query on a Hash field for a Mongoid Class. I'm not sure how I can do this with conditions?
这是一个例子:
class Person
include Mongoid::Document
field :things, :type => Hash
end
所以,假设我有这个:
p = Person.new
p.things = {}
p.things[:tv] = "Samsung"
我想查询第一个拥有三星电视的人...
I want to query for the first person with a tv that is a Samsung...
People.first(:conditions => ?????
提前致谢.
推荐答案
Person.where('things.tv' => 'Samsung').first
这是 Mongoid 和 MongoDB 真正闪耀的地方.Mongoid 的 Criteria 方法(Person.where
、Person.any_of
、Person.excludes
等)将为您提供比 ActiveRecord 更大的灵活性-样式查找器(将 :conditions
哈希传递给 Person.find
、Person.first
等)
This is where Mongoid and MongoDB really shine. Mongoid's Criteria methods (Person.where
, Person.any_of
, Person.excludes
, etc.) will give you much more flexibility than the ActiveRecord-style finders (passing a :conditions
hash to Person.find
, Person.first
, etc.)
Mongoid 的网站上有一些关于如何使用 Criteria
的精彩文档:
Mongoid's site has some great documentation on how to use Criteria
:
http://mongoid.org/en/mongoid/docs/querying.html
这篇关于查询 Mongoid 哈希字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!