Module: Mongoid::Findable

Extended by:
Forwardable
Defined in:
build/mongoid-7.3/lib/mongoid/findable.rb

Overview

This module defines the finder methods that hang off the document at the class level.

Since:

  • 4.0.0

Instance Method Summary collapse

Instance Method Details

#countInteger

Returns a count of records in the database. If you want to specify conditions use where.

Examples:

Get the count of matching documents.

Person.count
Person.where(title: "Sir").count

Returns:

  • (Integer)

    The number of matching documents.

Since:

  • 4.0.0



59
60
61
# File 'build/mongoid-7.3/lib/mongoid/findable.rb', line 59

def count
  with_default_scope.count
end

#empty?true, false

Returns true if count is zero

Examples:

Are there no saved documents for this model?

Person.empty?

Returns:

  • (true, false)

    If the collection is empty.

Since:

  • 4.0.0



79
80
81
# File 'build/mongoid-7.3/lib/mongoid/findable.rb', line 79

def empty?
  count == 0
end

#estimated_countInteger

Returns an estimated count of records in the database.

Examples:

Get the count of matching documents.

Person.estimated_count

Returns:

  • (Integer)

    The number of matching documents.

Since:

  • 4.0.0



69
70
71
# File 'build/mongoid-7.3/lib/mongoid/findable.rb', line 69

def estimated_count
  with_default_scope.estimated_count
end

#exists?true, false

Returns true if there are on document in database based on the provided arguments.

Examples:

Do any documents exist for the conditions?

Person.exists?

Returns:

  • (true, false)

    If any documents exist for the conditions.

Since:

  • 4.0.0



90
91
92
# File 'build/mongoid-7.3/lib/mongoid/findable.rb', line 90

def exists?
  with_default_scope.exists?
end

#find(*args) ⇒ Document | Array<Document> | nil

Finds a Document or multiple documents by their _id values.

If a single non-Array argument is given, this argument is interpreted as the _id value of a document to find. If there is a matching document in the database, this document is returned; otherwise, if the raise_not_found_error Mongoid configuration option is truthy (which is the default), Errors::DocumentNotFound is raised, and if raise_not_found_error is falsy, find returns nil.

If multiple arguments are given, or an Array argument is given, the array is flattened and each array element is interpreted as the _id value of the document to find. Mongoid then attempts to retrieve all documents with the provided _id values. The return value is an array of found documents. Each document appears one time in the returned array, even if its _id is given multiple times in the argument to find. If the raise_not_found_error Mongoid configuration option is truthy, Errors::DocumentNotFound exception is raised if any of the specified _ids were not found in the database. If the +raise_not_found_error Mongoid configuration option is falsy, only those documents which are found are returned; if no documents are found, the return value is an empty array.

Note that MongoDB does not allow the _id field to be an array.

The argument undergoes customary Mongoid type conversions based on the type declared for the _id field. By default the _id field is a BSON::ObjectId; this allows strings to be passed to find and the strings will be transparently converted to BSON::ObjectId instances during query construction.

The find method takes into account the default scope defined on the model class, if any.

Parameters:

  • args (Object | Array<Object>)

    The _id values to find or an array thereof.

Returns:

Raises:

  • Errors::DocumentNotFound If not all documents are found and the raise_not_found_error Mongoid configuration option is truthy.

Since:

  • 4.0.0



134
135
136
# File 'build/mongoid-7.3/lib/mongoid/findable.rb', line 134

def find(*args)
  with_default_scope.find(*args)
end

#find_by(attrs = {}) {|result| ... } ⇒ Document?

Find the first Document given the conditions. If a matching Document is not found and Mongoid.raise_not_found_error is true it raises Mongoid::Errors::DocumentNotFound, return null nil elsewise.

and Mongoid.raise_not_found_error is true.

Examples:

Find the document by attribute other than id

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

Parameters:

  • attrs (Hash) (defaults to: {})

    The attributes to check.

Yields:

  • (result)

Returns:

  • (Document, nil)

    A matching document.

Raises:

Since:

  • 3.0.0



154
155
156
157
158
159
160
161
# File 'build/mongoid-7.3/lib/mongoid/findable.rb', line 154

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

#find_by!(attrs = {}) {|result| ... } ⇒ Document

Find the first Document given the conditions, or raises Mongoid::Errors::DocumentNotFound

Examples:

Find the document by attribute other than id

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

Parameters:

  • attrs (Hash) (defaults to: {})

    The attributes to check.

Yields:

  • (result)

Returns:

Raises:

Since:

  • 4.0.0



175
176
177
178
179
180
# File 'build/mongoid-7.3/lib/mongoid/findable.rb', line 175

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

#firstDocument Also known as: one

Find the first Document given the conditions.

Examples:

Find the first document.

Person.first

Returns:

  • (Document)

    The first matching document.

Since:

  • 4.0.0



188
189
190
# File 'build/mongoid-7.3/lib/mongoid/findable.rb', line 188

def first
  with_default_scope.first
end

#lastDocument

Find the last Document given the conditions.

Examples:

Find the last document.

Person.last

Returns:

  • (Document)

    The last matching document.

Since:

  • 4.0.0



199
200
201
# File 'build/mongoid-7.3/lib/mongoid/findable.rb', line 199

def last
  with_default_scope.last
end