mongoid

19
Going NoSQL with Mongoid

Upload: luke-chadwick

Post on 10-Nov-2014

3.344 views

Category:

Technology


7 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Mongoid

Going NoSQL with Mongoid

Page 2: 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 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)

Page 3: Mongoid

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

Page 4: Mongoid

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.

Page 5: Mongoid

MongoDB Features•Full Index Support

•Replication and Scalability

•Auto-Sharding

•Querying

•Fast In-Place Updates

•Map/Reduce

Page 6: Mongoid

Installing MongoDBInstall 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

Page 7: Mongoid

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

Page 8: Mongoid

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

Page 9: Mongoid

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"])

Page 10: Mongoid

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/)

Page 11: Mongoid

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/)

Page 12: Mongoid

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

Page 13: Mongoid

Useful gems (continued)•https://github.com/adacosta/

mongoid_rails_migrations

•https://github.com/azisaka/mongoid_state_machine

Page 14: Mongoid

New application•rails new app_name –O –J –T –m http://bit.ly/

bV9gUn

Page 15: Mongoid

RSpecRSpec.configure do |config|

config.before :all do

Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)

end

endhttp://adventuresincoding.com/2010/07/how-to-configure-cucumber-and-rspec-to-work-with-mongoid/

Page 16: Mongoid

CucumberBefore do |scenario|

Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)

endhttp://adventuresincoding.com/2010/07/how-to-configure-cucumber-and-rspec-to-work-with-mongoid/

Page 17: Mongoid

Delayed Job# config/initializers/delayed_job_config.rb

Delayed::Worker.backend = :mongoid

Page 18: Mongoid

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)

Page 19: Mongoid

My Details•Luke Chadwick

•Blog: http://core.vertislabs.org

•Twitter: vertis

•Email: [email protected]