Going NoSQL with Mongoid
What do I want to share?
• What Mongoid is, and why you should use it.
• Some very brief examples of it’s capabilities
• A...
What is MongoDB?
• MongoDB is one of several possible ‘NoSQL’ Databases
• Other Examples might be: CouchDB, Tokyo Cabinet,...
Why MongoDB over X?
• Ease of installation
• Works on Mac, Windows, Linux
• Easy to develop with (e.g. no migrations)
• Ex...
MongoDB Features
• Full Index Support
• Replication and Scalability
• Auto-Sharding
• Querying
• Fast In-Place Updates
• M...
Installing MongoDB
Install the latest production version from mongodb.org
- Mongoid currently wants MongoDB 1.6.0 or great...
Mongoid vs. MongoMapper
• Mongoid is uses ActiveModel – MM is Rails2 focused
and used the validatable gem
• Mongoid provid...
Mongoid Features
• Plays nicely with Rails3 (Takes advantage of
ActiveModel)
• Excellent chainable query interface
• Quite...
Mongoid Features
(continued)• Mongoid supports Master/Slave replication clusters
• Writes to masters, round robin reads to...
Mongoid Associations
• embeds_one, embeds_many and embedded_in
• references_one, references_many, and referenced_in
• refe...
Querying using Mongoid
• Person.all(:conditions => { :first_name => "Syd" })
• Person.find(:all, :conditions => { :first_n...
Useful gems
• gem 'machinist_mongo', :require => 'machinist/mongoid',
:git =>
'http://github.com/nmerouze/machinist_mongo....
Useful gems (continued)
• https://github.com/adacosta/mongoid_rails_migrations
• https://github.com/azisaka/mongoid_state_...
New application
• rails new app_name –O –J –T –m http://bit.ly/bV9gUn
RSpec
RSpec.configure do |config|
config.before :all do
Mongoid.master.collections.select {|c| c.name !~
/system/ }.each(&...
Cucumber
Before do |scenario|
Mongoid.master.collections.select {|c| c.name !~ /system/
}.each(&:drop)
end
http://adventur...
Delayed Job
# config/initializers/delayed_job_config.rb
Delayed::Worker.backend = :mongoid
…
Resources
• MongoDB - http://www.mongodb.org
• Mongoid – http://www.mongoid.org
• Mongoid Source - http://github.com/mongo...
My Details
• Luke Chadwick
• Blog: http://core.vertislabs.org
• Twitter: vertis
• Email: luke.a.chadwick@gmail.com
Upcoming SlideShare
Loading in …5
×

Mongoid

3,192 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,192
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
30
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Mongoid

  1. 1. Going NoSQL with Mongoid
  2. 2. What do I want to share? • What Mongoid is, and why you should use it. • Some very brief examples of it’s capabilities • A setup that will allow you to follow bdd/tdd, while using Mongoid (i.e. some of the gems you take for granted don’t work without ActiveRecord)
  3. 3. What is MongoDB? • MongoDB is one of several possible ‘NoSQL’ Databases • Other Examples might be: CouchDB, Tokyo Cabinet, Voldemort, … • MongoDB falls into the ‘Document Database’ sub category of NoSQL
  4. 4. Why MongoDB over X? • Ease of installation • Works on Mac, Windows, Linux • Easy to develop with (e.g. no migrations) • Excellent Documentation (both MongoDB and Mongoid) • I’ve only used it on smaller projects so limitations that might appear at scale are not of concern to me.
  5. 5. MongoDB Features • Full Index Support • Replication and Scalability • Auto-Sharding • Querying • Fast In-Place Updates • Map/Reduce
  6. 6. Installing MongoDB Install the latest production version from mongodb.org - Mongoid currently wants MongoDB 1.6.0 or greater - (Debian/Ubuntu)Turn off listening on 0.0.0.0 or enable authentication
  7. 7. Mongoid vs. MongoMapper • Mongoid is uses ActiveModel – MM is Rails2 focused and used the validatable gem • Mongoid provides excellent support for embedded documents • Mongoid has better performance with large datasets and large documents • Mongoid has better documentation
  8. 8. Mongoid Features • Plays nicely with Rails3 (Takes advantage of ActiveModel) • Excellent chainable query interface • Quite a few existing gems have versions that support Mongoid • E.g. Delayed::Job and Devise can both use MongoDB through Mongoid
  9. 9. Mongoid Features (continued)• Mongoid supports Master/Slave replication clusters • Writes to masters, round robin reads to slaves • Great support for embedded documentsPerson.not_in(:status => ["Divorced", "Single"])
  10. 10. Mongoid Associations • embeds_one, embeds_many and embedded_in • references_one, references_many, and referenced_in • references_many :transactions vs references_many :transactions, :stored_as => :array (http://mongoid.org/docs/associations/)
  11. 11. Querying using Mongoid • Person.all(:conditions => { :first_name => "Syd" }) • Person.find(:all, :conditions => { :first_name => "Syd" }) • Person.not_in(:status => ["Divorced", "Single"]).only(:first_name, :last_name).limit(20) (http://mongoid.org/docs/querying/)
  12. 12. Useful gems • gem 'machinist_mongo', :require => 'machinist/mongoid', :git => 'http://github.com/nmerouze/machinist_mongo.git', :branch => 'machinist2’ • gem 'delayed_job', :git => 'http://github.com/collectiveidea/delayed_job.git', :branch => 'rails3-mailer' • gem 'delayed_job_mongoid', '>= 1.0.0.rc’ • gem 'mongoid-rspec’ # http://github.com/evansagge/mongoid-rspec
  13. 13. Useful gems (continued) • https://github.com/adacosta/mongoid_rails_migrations • https://github.com/azisaka/mongoid_state_machine
  14. 14. New application • rails new app_name –O –J –T –m http://bit.ly/bV9gUn
  15. 15. RSpec RSpec.configure do |config| config.before :all do Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop) end end http://adventuresincoding.com/2010/07/how-to-configure-cucumber-and-rspec-to-work-with-mongoid/
  16. 16. Cucumber Before do |scenario| Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop) end http://adventuresincoding.com/2010/07/how-to-configure-cucumber-and-rspec-to-work-with-mongoid/
  17. 17. Delayed Job # config/initializers/delayed_job_config.rb Delayed::Worker.backend = :mongoid …
  18. 18. Resources • MongoDB - http://www.mongodb.org • Mongoid – http://www.mongoid.org • Mongoid Source - http://github.com/mongoid/mongoid • My Rails Template - http://bit.ly/bV9gUn (There are probably more generic/complete templates out there)
  19. 19. My Details • Luke Chadwick • Blog: http://core.vertislabs.org • Twitter: vertis • Email: luke.a.chadwick@gmail.com

×