Mongoid

Post on 10-Nov-2014

3.335 views 7 download

Tags:

description

 

Transcript of Mongoid

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

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

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.

MongoDB Features•Full Index Support

•Replication and Scalability

•Auto-Sharding

•Querying

•Fast In-Place Updates

•Map/Reduce

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

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

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

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

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

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

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

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

mongoid_rails_migrations

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

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

bV9gUn

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/

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/

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

•My Rails Template - http://bit.ly/bV9gUn (There are probably more generic/complete templates out there)

My Details•Luke Chadwick

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

•Twitter: vertis

•Email: luke.a.chadwick@gmail.com