MongoDB & Mongoid with Rails
Upcoming SlideShare
Loading in...5
×
 

MongoDB & Mongoid with Rails

on

  • 23,953 views

Brown bag presentation on using MongoDB and Mongoid with Rails.

Brown bag presentation on using MongoDB and Mongoid with Rails.

Statistics

Views

Total Views
23,953
Views on SlideShare
23,612
Embed Views
341

Actions

Likes
26
Downloads
217
Comments
1

7 Embeds 341

http://www.slideshare.net 214
http://www.evalcode.com 97
http://paper.li 20
http://www.linkedin.com 6
http://webcache.googleusercontent.com 2
http://74.125.45.132 1
http://b.hatena.ne.jp 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • another useful link i found Rails and MongoDb
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

MongoDB & Mongoid with Rails MongoDB & Mongoid with Rails Presentation Transcript

  • MongoDB { name: “mongo”, type: “db” }
  • “MongoDB is designed to be human-oriented. It reduces the burden of programming. It tries to push jobs back to machines. You can accomplish more tasks with less work, in smaller yet readable code.” - Banker
  • built for speed document/collection oriented dynamic queries and indexes replication and failover
  • websites from yesterday and today high volume traffic high scalability storage of objects and JSON
  • caveats
  • no transactions
  • relationships
  • MongoDB with Ruby because we all want ActiveRecord...
  • mongo-ruby-driver mongomapper mongoid
  • Spiff uses Mongoid class Post include Mongoid::Document field :title, :type => String field :body, :type => String end @posts = @db.collection('posts') @document = {:title => "MongoDB on Rails", :body => "Revelatory! Loved it!"} @posts.save(@document)
  • Associations in Mongoid class Post include Mongoid::Document field :title, :type => String field :body, :type => String has_many :comments end class Comment include Mongoid::Document field :rant, :type => String belongs_to :post, :inverse_of => :comments end This creates an Embedded Document. @posts = @db.collection('posts') @document = {:title => "MongoDB on Rails", :body => "Revelatory! Loved it!", :comments => [{:rant => “I completely disagree”}] } @posts.save(@document)
  • Keys, Validations, Timestamps & Hooks class Post include Mongoid::Document include Mongoid::Timestamps field :title, :type => String field :body, :type => String field :slug, :type => String has_many :comments has_key :title, :unique => true # “Audi IMS” becomes “audiims” validates_presence_of :title, :body validates_unqiueness_of :title before_save :set_slug def set_slug self.slug = “#{title.parameterize}-#{body[0..3]}” end end
  • Versioning a Mongo Document class Post include Mongoid::Document include Mongoid::Timestamps include Mongoid::Versioning field :title, :type => String field :body, :type => String field :slug, :type => String has_many :comments has_key :title, :unique => true # “Audi IMS” becomes “audiims” validates_presence_of :title, :body validates_unqiueness_of :title before_save :set_slug def set_slug self.slug = “#{title.parameterize}-#{body[0..3]}” end end
  • Access related items class Post include Mongoid::Document include Mongoid::Timestamps include Mongoid::Versioning field :title, :type => String field :body, :type => String field :slug, :type => String has_many :comments belongs_to_related :account has_many_related :sources ..... end database.yml meet database.mongo.yml
  • But does it blend? Post.all(:conditions => { :title => “Rails 2.3” }) Post.find(:all, ...) Post.first(...) Post.find_or_create_by(:title => “Merb is dead”) Post.find_or_initialize_by(...) Associations: @post.comments.all
  • fuck SQL! Criteria is the shit. Criteria#all - perform exact matches Post.criteria.all(:title => [ “Rails”, “2.3” ]) Criteria#find - match key/value Post.criteria.and(:created_at.gt => 2.months.ago) Criteria#exclude Post.criteria.exclude(:created_at => Date.today) Criteria#id - match document id Post.criteria.id(“4b2fe28ee2dc9b5f7b000029”) Criteria#in - match any of the values in an array Post.criteria.in(:title => [“Merb”, “Rails”, “Ruby”] Criteria#order_by Post.criteria.order_by([[:created_at, :desc], [:title, :asc] ]) Criteria also has #limit, #not_in, #only, #skip
  • arithmetic, grouping & aggregation #max, #min, #sum, #aggregate, #group
  • “But what about named scopes dude?!” -Gabe named_scope :active, criteria.where(:active => true) do def count size end end named_scope :inactive, :where => { :active => false } named_scope :frags_over, lambda { |count| { :where => { :frags.gt => count } } } named_scope :deaths_under, lambda { |count| criteria.where(:deaths.lt => count) }
  • You can chain any of the Criteria API Post.in(:title => [“Rails”, “Ruby”]).where(:created_at.gt => 2.months.ago).skip(10) class Post include Mongoid::Document class << self def ruby_related criteria.in(:title => [“Ruby”, “Rails”]) end def in_last_two_months criteria.where(:created_at.gt => 2.months.ago)) end end end Post.ruby_related.in_last_two_months
  • additional mongoid features Composite Keys Indexing Inheritance native MongoDB expressions