模块:Mongoid::Findable
- 扩展方式:
- 可转发
- 定义于:
- lib/mongoid/findable.rb
Overview
此模块定义了在类级别挂起文档的查找器方法。
实例方法摘要折叠
-
#为空? ⇒ true | false
如果计数为零,则返回 true。
-
#estimated_count ⇒ 整数
返回数据库中的估计记录数。
-
#是否存在? (id_or_conditions = :none) ⇒ true | false
根据提供的参数,如果数据库中存在文档,则返回 true。
-
# find (*args, &block) ⇒ 文档 | Array<Document> | nil
根据_id值查找
Document
或多个文档。 -
# find_by (attrs = {}) {|result| ... } ⇒ 文档 | nil
在给定的条件下,找到第一个
Document
。 -
# find_by! (attrs = {}) {|result| ... } ⇒ 文档
在给定的条件下查找第一个
Document
,否则引发 Mongoid::Errors::DocumentNotFound。 -
# first (limit = nil) ⇒ 文档(也:#one)
在给定的条件下,找到第一个
Document
。 -
#last(limit = nil) ⇒ Document
在给定的条件下找到最后一个
Document
。
实例方法详细信息
#为空? ⇒ true | false
如果计数为零,则返回 true
94 95 96 |
# File 'lib/mongoid/findable.rb', 第94行 def 空? 数数 == 0 end |
#estimated_count ⇒整数
返回数据库中的估计记录数。
84 85 86 |
# File 'lib/mongoid/findable.rb', 第84行 def estimated_count with_default_scope.estimated_count end |
#是否存在? (id_or_conditions = :none) ⇒ true | false
根据提供的参数,如果数据库中存在文档,则返回 true。
115 116 117 |
# File 'lib/mongoid/findable.rb', 第115行 def 存在吗?(id_or_conditions = : none) with_default_scope.存在吗?(id_or_conditions) end |
# find (*args, &block) ⇒文档| Array< Document > | nil
每个参数都可以是单独的 ID、ID 数组或嵌套数组。 每个数组都将被展平。
根据_id值查找Document
或多个文档。
如果给出了单个非数组参数,则该参数被解释为要查找的文档的_id值。 如果数据库中有匹配的文档,则返回该文档;否则,如果raise_not_found_error
Mongoid 配置选项为 true(默认),则引发Errors::DocumentNotFound
;如果raise_not_found_error
为 false,则find
返回nil
。
如果给定了多个参数或给定了一个数组参数,则大量将被展平,并且每个大量元素都将被解释为要查找的文档的_id值。 然后,Mongoid 尝试检索具有所提供_id值的所有文档。 返回值是找到的文档的大量。 每个文档在返回的大量中都会出现一次,即使其_id在find
的参数中多次给出。 如果raise_not_found_error
Mongoid 配置选项为 true,则在数据库中找不到任何指定的 _id 时,会引发Errors::DocumentNotFound
异常。 如果raise_not_found_error
Mongoid 配置选项为 false,则仅返回找到的文档;如果未找到文档,则返回值为空大量。
请注意, MongoDB不允许_id字段是大量。
根据为_id字段声明的类型,该参数会进行惯用的 Mongoid 类型转换。 默认, _id字段是BSON::ObjectId
;这允许将字符串传递给find
,并且这些字符串将在查询构造期间透明地转换为BSON::ObjectId
实例。
如果为该方法提供了区块,则它会委托给Enumerable#find并返回当前 Crieria对象找到的第一个文档,并且该区块会为其返回真值。 如果同时给出了区块和 ID,则忽略该区块并返回给定 ID 的文档。 如果给定了区块和 Proc,则该方法将委托给Enumerable#find并使用 proc 作为默认。
find
方法会考虑模型类上定义的默认作用域(如果有)。
168 169 170 171 172 173 174 175 |
# File 'lib/mongoid/findable.rb', 第168行 def find(*args, 和块) empty_or_proc = args.空? || (args.长度 == 1 && args.first.is_a?(Proc)) if block_given? && empty_or_proc with_default_scope.find(*args, 和块) else with_default_scope.find(*args) end end |
# find_by (attrs = {}) {|result| ... } ⇒文档| nil
在给定的条件下,找到第一个Document
。 如果未找到匹配的 Document 且 Mongoid. Raise_not_Found_error 为 true,则会引发 Mongoid::Errors::DocumentNotFound,否则返回 null nil。
并且 Mongoid.raise_not_Found_error 为 true。
191 192 193 194 195 196 197 198 |
# File 'lib/mongoid/findable.rb', 第191行 def find_by(attrs = {}) 结果 = WHERE(attrs).find_first if 结果.nil? && Mongoid.Raise_not_Found_error 提高(Errors::DocumentNotFound.new(self, attrs)) end 产量(结果) if 结果 && block_given? 结果 end |
# find_by! (attrs = {}) {|result| ... } ⇒文档
在给定的条件下查找第一个Document
,否则引发 Mongoid::Errors::DocumentNotFound
211 212 213 214 215 216 |
# File 'lib/mongoid/findable.rb', 第211行 def find_by!(attrs = {}) 结果 = WHERE(attrs).find_first 提高(Errors::DocumentNotFound.new(self, attrs)) 除非 结果 产量(结果) if 结果 && block_given? 结果 end |
# first (limit = nil) ⇒文档也称为: one
在给定的条件下,找到第一个Document
。
226 227 228 |
# File 'lib/mongoid/findable.rb', 第226行 def first(limit = nil) with_default_scope.first(limit) end |
#last(limit = nil) ⇒ Document
在给定的条件下找到最后一个Document
。
239 240 241 |
# File 'lib/mongoid/findable.rb', 第239行 def last(limit = nil) with_default_scope.last(limit) end |