模块:Mongoid::Findable

扩展方式:
可转发
定义于:
lib/mongoid/findable.rb

Overview

此模块定义了在类级别挂起文档的查找器方法。

实例方法摘要折叠

实例方法详细信息

#为空?true | false

如果计数为零,则返回 true

例子:

该模型没有已保存的文档吗?

Person.empty?

返回:

  • ( true | false )

    如果集合为空。



94
95
96
# File 'lib/mongoid/findable.rb', 第94行

def 空?
  数数 == 0
end

#estimated_count整数

返回数据库中的估计记录数。

例子:

获取匹配文档的计数。

Person.estimated_count

返回:

  • ( Integer )

    匹配文档的数量。



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。

例子:

是否存在针对这些条件的任何文档?

Person.exists?

给定 _id 是否存在任何文档。

Person.exists?(BSON::ObjectId(...))

在给定条件下是否存在任何文档。

Person.exists?(name: "...")

参数:

  • id_or_conditions 哈希 | 对象 | false (默认为: :none

    要搜索的_id 、条件哈希、nil 或 false。

返回:

  • ( true | false )

    如果条件存在任何文档。 如果传递 nil 或 false,则始终为 false。



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方法会考虑模型类上定义的默认作用域(如果有)。

参数:

  • *args ( [ Object | Array<Object> ]... )

    要查找的 ID。

返回:

引发:

  • Errors::DocumentNotFound 如果未找到所有文档且raise_not_found_error Mongoid 配置选项为 true。



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。

例子:

按ID以外的属性查找文档

Person.find_by(:username => "superuser")

参数:

  • attrs 哈希 (默认为: {}

    要检查的属性。

产量:

  • (结果)

返回:

  • (Document | nil)

    匹配的文档。

引发:



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

例子:

按ID以外的属性查找文档

Person.find_by(:username => "superuser")

参数:

  • attrs 哈希 (默认为: {}

    要检查的属性。

产量:

  • (结果)

返回:

  • (文档)

    匹配的文档。

引发:



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

例子:

查找第一个文档。

Person.first

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (文档)

    第一个匹配文档。



226
227
228
# File 'lib/mongoid/findable.rb', 第226行

def first(limit = nil)
  with_default_scope.first(limit)
end

#last(limit = nil) ⇒ Document

在给定的条件下找到最后一个Document

例子:

查找最后一个文档。

Person.last

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (文档)

    最后一个匹配的文档。



239
240
241
# File 'lib/mongoid/findable.rb', 第239行

def last(limit = nil)
  with_default_scope.last(limit)
end