模型.查找 mongoid 2.x.


可以

mongoid 2.x此查询吗?

User.find(username: "hyperrjas")

我收到错误:

Mongoid::Errors::DocumentNotFound: Document not found for class User with id(s) {:username=> "hyperrjas"}.

在 mongoid 3.x 中可以使用Model.find_by(username: "hyperrjas")

作为我使用的mongoid 2.x替代方案

Model.find_or_create_by(username: "hyperrjas")

但我的问题是,如果可能的话,Model.find(username: "hyperrjas")与Mongoid 2.x一起使用

使用 where 方法而不是 find。查找要求输入 ID。

获取更多信息 : http://two.mongoid.org/docs/querying/criteria.html

(对于 mongoid 3.x : http://mongoid.org/en/mongoid/docs/querying.html)

最新更新