模块:Mongoid::Findable
- 扩展方式:
- 可转发
- 定义于:
- lib/mongoid/findable.rb
Overview
此模块定义了在类级别挂起文档的查找器方法。
实例方法摘要折叠
-
# 任何? ⇒ true | false
如果条件中存在任何 document,则返回 true。
-
#count ⇒ Integer
返回数据库中的记录数。
-
#为空? ⇒ 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
如果条件中存在多个 document,则返回 true。
-
# one? ⇒ true | false
如果条件中只存在一个 document,则返回 true。
实例方法详细信息
# 任何? ⇒ true | false
如果条件中存在任何 document,则返回 true。
124 125 126 |
# File 'lib/mongoid/findable.rb', 第124行 def 任何? limit(1).数数 > 0 end |
#count ⇒ Integer
返回数据库中的记录数。 如果要指定条件,请使用 where。
73 74 75 |
# File 'lib/mongoid/findable.rb', 第73行 def 数数 with_default_scope.数数 end |
#为空? ⇒ true | false
如果计数为零,则返回 true
93 94 95 |
# File 'lib/mongoid/findable.rb', 第93行 def 空? 数数 == 0 end |
#estimated_count ⇒整数
返回数据库中的估计记录数。
83 84 85 |
# File 'lib/mongoid/findable.rb', 第83行 def estimated_count with_default_scope.estimated_count end |
#是否存在? (id_or_conditions = :none) ⇒ true | false
根据提供的参数,如果数据库中存在文档,则返回 true。
114 115 116 |
# File 'lib/mongoid/findable.rb', 第114行 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 并返回当前 Criteria对象找到的第一个 document,而该区块会为其返回真值。如果同时给出了区块和 ID,则忽略该区块并返回给定 ID 的 document。如果给定了区块和过程,则该方法将委托给 Enumerable#find 并使用该过程作为默认。
find方法会考虑模型类上定义的默认作用域(如果有)。
197 198 199 200 201 202 203 204 |
# File 'lib/mongoid/findable.rb', 第197行 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。
220 221 222 223 224 225 226 |
# File 'lib/mongoid/findable.rb', 第220行 def find_by(attrs = {}) 结果 = WHERE(attrs).find_first 提高(Errors::DocumentNotFound.new(self, attrs)) if 结果.nil? && Mongoid.Raise_not_Found_error 产量(结果) if 结果 && block_given? 结果 end |
# find_by! (attrs = {}) {|result| ... } ⇒文档
在给定的条件下查找第一个Document ,否则引发 Mongoid::Errors::DocumentNotFound
239 240 241 242 243 244 245 |
# File 'lib/mongoid/findable.rb', 第239行 def find_by!(attrs = {}) 结果 = WHERE(attrs).find_first 提高(Errors::DocumentNotFound.new(self, attrs)) 除非 结果 产量(结果) if 结果 && block_given? 结果 end |
# first (limit = nil) ⇒文档也称为: one
在给定的条件下,找到第一个Document 。
255 256 257 |
# File 'lib/mongoid/findable.rb', 第255行 def first(limit = nil) with_default_scope.first(limit) end |
#last(limit = nil) ⇒ Document
在给定的条件下找到最后一个Document 。
268 269 270 |
# File 'lib/mongoid/findable.rb', 第268行 def last(limit = nil) with_default_scope.last(limit) end |
#很多? ⇒ true | false
如果条件中存在多个 document,则返回 true。
144 145 146 |
# File 'lib/mongoid/findable.rb', 第144行 def 很多? limit(2).数数 > 1 end |
# one? ⇒ true | false
如果条件中只存在一个 document,则返回 true。
134 135 136 |
# File 'lib/mongoid/findable.rb', 第134行 def 一个? limit(2).数数 == 1 end |