extras

Mongoid has some useful extra features that can be used in applications.

Master / Slave Support

If slave databases are supplied in the mongoid.yml, Mongoid will round-robin all enslaved documents' read queries to the slave databases. This can also be handled on a per-query basis.

To have all reads for a model round robin between slaves enslave the class, note there is no need to do this in a replica set environment that set the read_secondary config option to true.

class Person
  include Mongoid::Document
  enslave
end

To send reads to the slaves on a per-query basis just enslave a Criteria:

Person.where(first_name: "Omar").enslave

Caching

Out of the box, Mongoid wraps the MongoDB Ruby Driver's cursor in order to be memory efficient for large queries and data sets. However if you want the query to load all matching documents in memory and return them on n iterations without hitting the database again you may cache a class or criteria:

To have all queries for a model "cache":

class Person
  include Mongoid::Document
  cache
end

To cache on a per-query basis:

Person.where(first_name: "Franziska").cache

Paranoid Documents

There may be times when you don't want documents to actually get deleted from the database, but "flagged" as deleted. Mongoid provides a Paranoia module to give you just that.

class Person
  include Mongoid::Document
  include Mongoid::Paranoia
end

person.delete # Sets the deleted_at field to the current time.
person.delete! # Permanently deletes the document.
person.destroy! # Permanently delete the document with callbacks.
person.restore # Brings the "deleted" document back to life.

Versioning

Mongoid supports simple versioning through inclusion of the Mongoid::Versioning module. Including this module will create a versions embedded relation on the document that it will append to on each save. It will also update the version number on the document, which is an integer.

class Person
  include Mongoid::Document
  include Mongoid::Versioning
end

You can also set a max_versions setting, and Mongoid will only keep the max most recent versions.

class Person
  include Mongoid::Document
  include Mongoid::Versioning

  # keep at most 5 versions of a record
  max_versions 5
end

You may skip versioning at any point in time by wrapping the persistence call in a versionless block.

person.versionless do |doc|
  doc.update_attributes(name: "Theodore")
end

Timestamping

Mongoid supplies a timestamping module in Mongoid::Timestamps which can be included to get basic behavior for created_at and updated_at fields.

class Person
  include Mongoid::Document
  include Mongoid::Timestamps
end

You may also choose to only have specific timestamps for creation or modification.

class Person
  include Mongoid::Document
  include Mongoid::Timestamps::Created
end

class Post
  include Mongoid::Document
  include Mongoid::Timestamps::Updated
end

If you want to turn off timestamping for specific calls, use the timeless method:

person.timeless.save
Person.timeless.create!

Keys

You can create a composite key in mongoid to replace the default id using the key macro:

class Person
  include Mongoid::Document
  field :first_name
  field :last_name
  key :first_name, :last_name
end

person = Person.new(first_name: "Syd", last_name: "Vicious")
person.id # returns "syd-vicious"