finders

Mongoid supports more traditional-style finders for those who prefer an Active Record 2 type syntax. Note that unlike criteria, these methods are not chainable and return actual arrays of documents, or a single document, with the exception of those noted in their individual sections.

finder methods

Model.all

Returns a chainable criteria object with the provided conditions applied.

# Find all people by MongoDB compatible conditions and options.
Person.all
Person.all(conditions: { first_name: /^dur/i, "addresses.city" => "Berlin" })
Person.all(conditions: { title: "Sir" }, limit: 5)
Person.all(sort: [[ :first_name, :asc ]])

Model.count

Returns the number of documents in the database. If you want to specify conditions use where.

# Get the count of documents.
Person.count
# Get the count of documents given the provided conditions.
Person.where(title: "Sir").count

Model.exists?

Returns true if any documents in the database exist given the provided conditions and false if there are none.

# Do any documents exist in the database for the provided conditions?
Person.exists?
Person.exists?(conditions: { title: "Sir" })

Model.find | Criteria#find

Provides the ability to find a document or documents given a set of conditions or ids. This either returns a criteria, an array of documents, or a single document depending on the arguments passed.

The first way to use this method is for finding a single document given the provided ID. If no document is found this will raise an error unless the configuration option is changed. You can provide a BSON::ObjectId or its string representation (Mongoid will convert to the appropriate type). This can also be placed at the end of a criteria chain to limit by a single document.

Person.find(id)
Person.find("4baa56f1230048567300485c")
Person.where(last_name: "Black").find(id)

You may find multiple documents given the provided array of ids. If a single document is not found the error will get raised. You may provide object ids or string representations. This can also be placed at the end of a criteria chain to find specific documents.

Person.find([id_one, id_two])
Person.find(["4baa56f1230048567300485c","4baa56f1230048567300485d"])
Person.where(last_name: "Black").find([ id_one, id_two ])

Model.find_or_create_by

Find a document given the provided conditions, or create a new one if nothing is persisted.

Person.find_or_create_by(first_name: "Syd", last_name: "Vicious")

Model.find_or_initialize_by

Find a document given the provided conditions, or instantiate a new one if nothing is persisted.

Person.find_or_initialize_by(first_name: "Syd", last_name: "Vicious")

Model.first

Find the first document in the database given the provided conditions. Will return a document or nil if nothing is found and defaults to the natural sorting of documents in the database. You can provide sort criteria as well if you want to dictate the exact document that would be returned first.

Person.first(conditions: { first_name: "Syd" })
Person.first(sort: [[ :first_name, :asc ]])

Model.last

Find the last document in the database given the provided conditions. Will return a document or nil if nothing is found and defaults to to sorting by id in descending order. You may provide sort criteria as well if you want to dictate the exact document that would be returned - Mongoid will invert the sort criteria you provide.

Person.last(conditions: { first_name: "Syd" })
Person.last(sort: [[ :first_name, :asc ]])