2016-03-22 30 views
0

新手到mongoid軌道。其中()。首先與對mongoid的find_by

是否有使用

Model.where(:name => "XYZ").first 

Model.find_by(:name => "XYZ") 

我在這兩個查詢在查詢中使用「限制1」控制檯中看到之間的任何性能影響差異(當我在Postgres的使用)。這在Mongoid中是一樣的嗎?

回答

2

看來find_by採用地方和國內第一

def find_by(attrs = {}) 
    result = where(attrs).find_first 
    if result.nil? && Mongoid.raise_not_found_error 
    raise(Errors::DocumentNotFound.new(self, attrs)) 
    end 
    yield(result) if result && block_given? 
    result 
end