Overview
About Mongoid
Mongoid is an ODM (Object-Document-Mapper) framework for MongoDB in Ruby.
Project Tracking
Mongoid on Pivotal Tracker Mongoid Google Group Mongoid on CI Joe
Compatibility
Mongoid is developed against Ruby 1.8.6, 1.8.7, 1.9.1, 1.9.2
Note API changes will be frequent until the 1.0 release, and do not expect the gem to be of full production quality until that point.
Mongoid in Action
Initialize Mongoid:
Mongoid.connect_to("myapp_database_name")
Example of a simple domain model:
class Person < Mongoid::Document
field :title
field :age, :default => 0
has_many :addresses
has_one :name
end
class Address < Mongoid::Document
field :street
field :city
field :state
field :post_code
belongs_to :person
end
class Name < Mongoid::Document
field :first_name
field :last_name
end
Mongoid suuports all the basic ActiveRecord creation and persistence methods.
Creation:
Person.create(:title => "Esquire")
Person.create!(:title => "Sir", :name => { :first_name => "Elton", :last_name => "John" })
Save:
person.save
person.save!
Updating:
person.update_attributes(:title => "Sir")
person.update_attributes!(:name => { :first_name => "Emmanuel", :last_name => "Zorg" })
Deleting:
person.destroy
Person.destroy_all(:title => "Sir")
person.delete
Person.delete_all(:title => "Sir")
Retrieval of Documents from the database can be done in the traditional ActiveRecord manner or done using the Mongoid criteria DSL.
Old School:
Person.find(:all, :conditions => { :title => "Esquire" })
Person.find(:first, :conditions => { :title => "Esquire" })
Person.first(:conditions => { :title => "Sir" })
Person.all(:conditions => { :title => "Sir" })
New School:
Person.select(:first_name, :last_name).where(:title => "Sir").skip(10).limit(10).execute
Criteria.translate(:conditions => { :title => "Sir" }).execute
Paginate Document search results:
Person.paginate(:conditions => {:title => "Esquire"}, :page => 1, :per_page => 20)
Validations:
Mongoid supports all validations provided by Jay Fields’ Validatable gem. For more information please see the Validatable documentation.
License
Copyright © 2009 Durran Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Credits
Durran Jordan: durran at gmail dot com