2012-12-10 31 views
0

可能在mongoid 2.x這個查詢嗎?Model.find 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

回答

0

使用該方法代替find。尋找一個id。

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

(對於mongoid 3.X:http://mongoid.org/en/mongoid/docs/querying.html

+0

謝謝'where'返回一個標準。我不需要一個標準。我需要發現者。 – hyperrjas

+0

然後添加.first以獲取行集的第一個結果。你也可以使用find_by,但是一切都在文檔中。 –

+0

謝謝,mongoid 2.x的最佳選項是'User.first(條件:{username:「hyperrjas」})''。 – hyperrjas