durran / mongoid
- Source
- Commits
- Network (15)
- Issues (1)
- Downloads (98)
- Wiki (1)
- Graphs
-
Tree:
8702f02
tree 12a54ef66c1fb40c8bc45eeac79e68625810315a
parent 43f63f5f9150e89f7e4695e4801f6420084ceea0
| name | age | message | |
|---|---|---|---|
| |
.gitignore | Thu Oct 01 16:41:47 -0700 2009 | |
| |
.watchr | Sun Oct 18 13:09:44 -0700 2009 | |
| |
MIT_LICENSE | Wed Aug 26 14:36:43 -0700 2009 | |
| |
README.textile | ||
| |
Rakefile | ||
| |
VERSION | ||
| |
lib/ | ||
| |
mongoid.gemspec | ||
| |
spec/ |
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
include Mongoid::Versioning
field :title
field :age, :type => Integer, :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
include Mongoid::Timestamps
field :first_name
field :last_name
end
Mongoid supports 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")
Count:
Person.count
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
Person.select(:title).aggregate
Criteria.translate(:conditions => { :title => "Sir" }).execute
Paginate Document search results:
Person.paginate(:conditions => {:title => "Esquire"}, :page => 1, :per_page => 20)
Versioning:
Versioning can be added by including Mongoid::Versioning in your document. This will give
a version field, and will update the version number with each save. Additionally each
version of the document will be stored in a versions array, embedded within the document.
class Person < Mongoid::Document
include Mongoid::Versioning
field :title
end
@person = Person.new
@person.save # Version 1
@person.title = "Madam"
@person.save # Version 2
@person.versions.size = 1
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

