【问题标题】:Model.find mongoid 2.x模型.find mongoid 2.x
【发布时间】:2012-12-10 18:51:33
【问题描述】:

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 一起使用

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongoid


    【解决方案1】:

    使用 where 而不是 find 的方法。 Find 请求一个 id。

    欲了解更多信息:http://two.mongoid.org/docs/querying/criteria.html

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

    【讨论】:

    • 谢谢where返回一个标准。我不需要一个标准。我需要发现者。
    • 然后添加 .first 以获取行集的第一个结果。您也可以使用 find_by,但一切都在文档中。
    • 谢谢,mongoid 2.x 的最佳选择是User.first(conditions: {username: "hyperrjas"})
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    相关资源
    最近更新 更多