Module: Mongoid::Findable

Extended by:
Origin::Forwardable
Defined in:
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


53
54
55
# File 'lib/mongoid/findable.rb', line 53

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


63
64
65
# File 'lib/mongoid/findable.rb', line 63

def empty?
  count == 0
end

#exists?Boolean

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

Examples:

Do any documents exist for the conditions?

Person.exists?

Parameters:

  • args (Array)

    The conditions.

Returns:

Since:

  • 4.0.0


74
75
76
# File 'lib/mongoid/findable.rb', line 74

def exists?
  with_default_scope.exists?
end

#find(*args) ⇒ Document, ...

Find a Document in several different ways.

If a String is provided, it will be assumed that it is a representation of a Mongo::ObjectID and will attempt to find a single Document based on that id. If a Symbol and Hash is provided then it will attempt to find either a single Document or multiples based on the conditions provided and the first parameter.

Examples:

Find a single document by an id.

Person.find(BSON::ObjectId)

Parameters:

  • args (Array)

    An assortment of finder options.

Returns:

Since:

  • 4.0.0


92
93
94
# File 'lib/mongoid/findable.rb', line 92

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


112
113
114
115
116
117
118
119
# File 'lib/mongoid/findable.rb', line 112

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


133
134
135
136
137
138
# File 'lib/mongoid/findable.rb', line 133

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


146
147
148
# File 'lib/mongoid/findable.rb', line 146

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


157
158
159
# File 'lib/mongoid/findable.rb', line 157

def last
  with_default_scope.last
end