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:
To send reads to the slaves on a per-query basis just enslave a Criteria:
To have all reads for a model round robin between slaves:
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 => "Durran").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":
To cache on a per-query basis:
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 => "Durran").cache
Dirty Attributes
Mongoid implements an ActiveModel style dirty attributes module that conforms to the
ActiveModel API exactly:
class Person
include Mongoid::Document
field :first_name
end
person = Person.new(:first_name => "Leroy")
person.first_name = "Lauren"
person.changed? # true
person.first_name_changed? # true
person.first_name_was # Leroy
person.first_name_change # [ "Leroy", "Lauren" ]
person.changes # { "first_name" => [ "Leroy", "Lauren" ] }
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 association 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
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
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"
Persisting to Different Collections.
Simple, use the store_in macro.
class Person
include Mongoid::Document
store_in :students
end