Mongoid 查询的相等运算符

Mongoid query's equal operator

我想获得数据库中第一个 42 岁的人。

我怎样才能使这个查询有效:

Person.where(:Age.eq => 42).first

我得到的不是查询结果,而是:

undefined method `eq' for :Age:Symbol

只是:

Person.where(Age: 42).first

?

试一试:-

Person.where(:age => 42).first

它将查询具有 age = 42

Person

这样查询:-

Person.find_by age: 42

find_by方法找到符合此条件的第一条记录

See the documentation