Mongoid is an ODM (Object-Document-Mapper) framework for MongoDB in Ruby. Mongoid differs from other mapping frameworks in that it constrains the models into behaving in a manner appropriate for a document database. That is to say there are no relationships between documents in the underlying datastore. If a relationship is set up in the Model the child models are automatically embedded within the parent document in the database. The concept of a foreign key relationship to another Document does not exist, as that is design and thinking for a relational database, not a document database. Mongoloid does however provide all the ActiveRecord style functionality you need, with the difference that it stores all associations within the parent document.
Mongoid is developed against Ruby 1.8.6, 1.8.7, 1.9.1
Example of a simple domain model:
class Person < Mongoid::Document fields :title has_many :addresses has_one :name end class Address < Mongoid::Document fields \ :street, :city, :state, :post_code belongs_to :person end class Name < Mongoid::Document fields \ :first_name, :last_name end
Create a new Document:
Person.create(:title => "Esquire")
Save a Document:
Delete a Document:
Update a Document:
person.update_attributes(:title => "Sir")
Search for a Document in the database:
Person.find(:all, :title => "Esquire") Person.find(:first, :title => "Esquire")
Paginate Document search results:
Person.paginate(:title => "Esquire", :page => 1, :per_page => 20)
Mongoid supports all validations provided by Jay Fields’ Validatable gem.
For more information please see the Validatable documentation.
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.
Durran Jordan: durran at gmail dot com