Module | Mongoid::Finders |
In: |
lib/mongoid/finders.rb
|
This module defines the finder methods that hang off the document at the class level.
Returns true if count is zero
@example Are there no saved documents for this model?
Person.empty?
@return [ true, false ] If the collection is empty.
Returns true if there are on document in database based on the provided arguments.
@example Do any documents exist for the conditions?
Person.exists?(:conditions => { :attribute => "value" })
@param [ Array ] args The conditions.
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.
@example Find the first matching document.
Person.find(:first, :conditions => { :attribute => "value" })
@example Find all matching documents.
Person.find(:all, :conditions => { :attribute => "value" })
@example Find a single document by an id.
Person.find(BSON::ObjectId)
@param [ Array ] args An assortment of finder options.
@return [ Document, nil, Criteria ] A document or matching documents.
Find the first Document given the conditions, or creates a new document with the conditions that were supplied.
@example Find or create the document.
Person.find_or_create_by(:attribute => "value")
@param [ Hash ] attrs The attributes to check.
@return [ Document ] A matching or newly created document.
Find the first Document given the conditions, or initializes a new document with the conditions that were supplied.
@example Find or initialize the document.
Person.find_or_initialize_by(:attribute => "value")
@param [ Hash ] attrs The attributes to check.
@return [ Document ] A matching or newly initialized document.