SlideShare a Scribd company logo
Mongoid in the
Real World


Toronto Ruby Brigade           Kevin Faustino
December 14, 2010                         @kfaustino
                       http://adventuresincoding.com
Thanks to our sponsors
What is Mongoid?
Object Document
    Mapper
Mongoid in the real world
Using Mongoid
Everyday Mongoid
   Integrations
class Post
end
class Post
  include Mongoid::Document
end
Types
class Post
  include Mongoid::Document
  include Mongoid::Timestamps

  field   :title
  field   :content
  field   :published_on, :type => DateTime
  field   :slug
end
Valid Types



Array, BigDecimal, Boolean, Date, DateTime, Float, Hash,
Integer, String, Symbol, Time
Associations
class Person
  include Mongoid::Document
  field :first_name
  field :last_name
  embeds_one :address
end

class Address
  include Mongoid::Document
  field :street
  field :city
  embedded_in :person, :inverse_of => :address
end
{
    "_id" : ObjectId("4d06eaa46c50a1031a000001"),
    "address" : {
       "_id" : ObjectId("4d06eade6c50a1031a000002"),
       "street" : "123 Liberty St.",
       "city" : "Toronto"
    },
    "first_name" : "Kevin",
    "last_name" : "Faustino"
}
class Blog
  include Mongoid::Document

  references_many :posts
end

class Post
  include Mongoid::Document

  referenced_in :blog
end
{
  "_id" : ObjectId("4c9f54866c50a10a75000003"),
  "blog_id" : ObjectId("4ca531c46c50a12d1b000002"),
  "content" : "What is Faraday? ...",
  "featured_image_filename" : "faraday.jpg",
  "slug" : "building-modular-http-client-code-with-
faraday",
  "title" : "Building modular HTTP client code with
Faraday",
}
Validations
ActiveModel Validators + More
       validates_acceptance_of
       validates_associated
       validates_confirmation_of
       validates_exclusion_of
       validates_format_of
       validates_inclusion_of
       validates_length_of
       validates_numericality_of
       validates_presence_of
       validates_uniqueness_of
Indexing
Defining Indexes
# unique
index :ssn, :unique => true

# Compound indexes
index([
     [ :first_name, Mongo::ASCENDING ],
     [ :last_name, Mongo::ASCENDING ]
])

# Foreign Key indexes
referenced_in :post, :inverse_of
=> :comments, :index => true
Using Mongoid
Everyday Mongoid
   Integrations
Document Limit
4 MB
Mongoid in the real world
Testing
RSpec Integration

# in spec/spec_helper.rb
RSpec.configure do |config|

  config.mock_with :rspec

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

end
Shoulda Matchers?
Use Remarkable


# in spec/spec_helper.rb
require 'remarkable/active_model'
require 'remarkable/mongoid'
Mongoid Matchers
have_field
reference_one
reference_many
be_referenced_in
embed_one
embed_many
be_embedded_in
validate_uniqueness_of
validate_association
All ActiveModel validations from Remarkable::ActiveModel
Cucumber Integration


# features/support/hooks.rb
Before do |scenario|
  Mongoid.master.collections.select {|c| c.name !~ /system/ }.each
(&:drop)
end
Factory Girl
Works out of the box
FactoryGirl.define do

  factory :post do
    title 'My first blog post'
    author 'Kevin Faustino'
    content 'Hello World'
  end

end
Factories with Cucumber


# features/support/env.rb
require 'factory_girl'
require 'factory_girl/step_definitions'
Monkey Patched Step Definition

# features/step_definitions/mongoid_steps.rb
Given /^an? (.+) exists with an? (.+) of "([^"]*)"$/
do |model, field, value|
  factory_name = model.gsub(' ', '_')
  Factory factory_name, field => value
end
Example Feature Usage


Given the following posts exist:
  | title                        | excerpt          |
  | 10 greatest Batman Villians | We countdown ... |
  | The Death of Batman          | Batman falls ... |
Caching in Rails
Create a Cache-Key
module Cacheable

  def cache_key
    if new_record?
      "#{collection_name}/#{id}/new"
    elsif respond_to?(:updated_at)
      "#{collection_name}/#{id}-#{updated_at.to_i}"
    else
      "#{collection_name}/#{id}"
    end
  end

end
Safe Operations
post = Post.safely(:w => 2).create(
  :title => 'We will receive verification'
)
post.safely(:w => 2).destroy
Safely Options


:w - A client can block until a write
operation has been replicated to N servers

:fsync - force the database to fsync all files
before returning
Enslave
Query from your Slaves

# Query specific
Post.where(:title => 'Toronto Ruby').enslave

# Document specific
class Post
  include Mongoid::Document
  enslave
end
Dropping Down
When you need more
performance, drop down
      to the driver
collection = mongo-ruby-driver


# Add a post id only if it does not already exist on
# the set
collection.update({'_id' => id}, {'$addToSet' =>
{'post_ids' => post.id}})
Using Mongoid
Everyday Mongoid
   Integrations
Full Text-Search
Use Solr

@solr = RSolr.connect :url => "http://127.0.0.1:8983/solr"
@solr.add( {
  :id => post.id,
  :text => post.content,
  :published_on => post.published_on.strftime("%Y-%m-%dT%H:%M:
00Z")
  }, { :add_attributes => { :commitWithin => 10000 } })
Photo Uploads
Not with paperclip!




      X
Mongoid in the real world
Agnostic
Document Example
class Post
  include Mongoid::Document
  include Mongoid::Timestamps

  field   :title
  field   :author
  field   :content
  field   :slug

  referenced_in :blog

  mount_uploader :featured_image, ImageUploader
end
Authentication
Use Devise

class User
  include Mongoid::Document
  devise :database_authenticatable,
    :rememberable,
    :trackable,
    :validatable

end
Coming Soon
Many to Many support
without dropping down
     to the driver
Thank you!

More Related Content

KEY
MongoDB & Mongoid with Rails
Justin Smestad
 
PDF
MongoDB and Ruby on Rails
rfischer20
 
PDF
ActiveRecord vs Mongoid
Ivan Nemytchenko
 
PDF
Using Mongoid with Ruby on Rails
Nicholas Altobelli
 
KEY
Practical Ruby Projects With Mongo Db
Alex Sharp
 
PPTX
Building Your First Application with MongoDB
MongoDB
 
PDF
Building your first app with mongo db
MongoDB
 
PDF
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Steven Francia
 
MongoDB & Mongoid with Rails
Justin Smestad
 
MongoDB and Ruby on Rails
rfischer20
 
ActiveRecord vs Mongoid
Ivan Nemytchenko
 
Using Mongoid with Ruby on Rails
Nicholas Altobelli
 
Practical Ruby Projects With Mongo Db
Alex Sharp
 
Building Your First Application with MongoDB
MongoDB
 
Building your first app with mongo db
MongoDB
 
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Steven Francia
 

What's hot (20)

PDF
Learn Learn how to build your mobile back-end with MongoDB
Marakana Inc.
 
PDF
Building Apps with MongoDB
Nate Abele
 
PPTX
Webinar: Getting Started with MongoDB - Back to Basics
MongoDB
 
PPT
Building Your First MongoDB App ~ Metadata Catalog
hungarianhc
 
KEY
Schema Design with MongoDB
rogerbodamer
 
PDF
Building Your First MongoDB App
Henrik Ingo
 
PPTX
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
MongoDB
 
KEY
Schema Design by Example ~ MongoSF 2012
hungarianhc
 
PPT
Advanced Json
guestfd7d7c
 
KEY
MongoDB
Steven Francia
 
PPTX
Webinar: Back to Basics: Thinking in Documents
MongoDB
 
PPTX
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
PDF
10gen Presents Schema Design and Data Modeling
DATAVERSITY
 
PPTX
Back to Basics Webinar 5: Introduction to the Aggregation Framework
MongoDB
 
PPTX
Back to Basics Webinar 1 - Introduction to NoSQL
Joe Drumgoole
 
PPTX
Back to Basics Webinar 3 - Thinking in Documents
Joe Drumgoole
 
ODP
MongoDB : The Definitive Guide
Wildan Maulana
 
PPTX
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
MongoDB
 
PPTX
Building Your First App: An Introduction to MongoDB
MongoDB
 
PPTX
Back to Basics Webinar 3: Schema Design Thinking in Documents
MongoDB
 
Learn Learn how to build your mobile back-end with MongoDB
Marakana Inc.
 
Building Apps with MongoDB
Nate Abele
 
Webinar: Getting Started with MongoDB - Back to Basics
MongoDB
 
Building Your First MongoDB App ~ Metadata Catalog
hungarianhc
 
Schema Design with MongoDB
rogerbodamer
 
Building Your First MongoDB App
Henrik Ingo
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
MongoDB
 
Schema Design by Example ~ MongoSF 2012
hungarianhc
 
Advanced Json
guestfd7d7c
 
Webinar: Back to Basics: Thinking in Documents
MongoDB
 
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
10gen Presents Schema Design and Data Modeling
DATAVERSITY
 
Back to Basics Webinar 5: Introduction to the Aggregation Framework
MongoDB
 
Back to Basics Webinar 1 - Introduction to NoSQL
Joe Drumgoole
 
Back to Basics Webinar 3 - Thinking in Documents
Joe Drumgoole
 
MongoDB : The Definitive Guide
Wildan Maulana
 
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
MongoDB
 
Building Your First App: An Introduction to MongoDB
MongoDB
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
MongoDB
 
Ad

Similar to Mongoid in the real world (20)

PDF
Mongodb mongoid
Marcos Vanetta
 
PPTX
Simple MongoDB design for Rails apps
Sérgio Santos
 
PDF
Mongodb
Scott Motte
 
PPTX
Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)
ecommerce poland expo
 
PDF
From Zero to Mongo, Art.sy Experience w/ MongoDB
Daniel Doubrovkine
 
PDF
Webinar: Getting Started with Ruby and MongoDB
MongoDB
 
KEY
The Ruby/mongoDB ecosystem
Harold Giménez
 
KEY
MongoDB at RubyEnRails 2009
Mike Dirolf
 
PDF
Ruby Development and MongoMapper (John Nunemaker)
MongoSF
 
PDF
Rails Loves MongoDB
BillWatts
 
KEY
MongoDB
Steve Klabnik
 
PDF
MongoDB at FrozenRails
Mike Dirolf
 
KEY
MongoDB at ZPUGDC
Mike Dirolf
 
PDF
Mongo db
Antonio Terreno
 
PPTX
Rails with MongoDB - RORLab 47th
Eugene Park
 
PDF
Tame Accidental Complexity with Ruby and MongoMapper
Giordano Scalzo
 
KEY
MongoMapper lightning talk
Kerry Buckley
 
PDF
Mongo db eveningschemadesign
MongoDB APAC
 
KEY
MongoDB at RuPy
Mike Dirolf
 
PPTX
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
Mongodb mongoid
Marcos Vanetta
 
Simple MongoDB design for Rails apps
Sérgio Santos
 
Mongodb
Scott Motte
 
Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)
ecommerce poland expo
 
From Zero to Mongo, Art.sy Experience w/ MongoDB
Daniel Doubrovkine
 
Webinar: Getting Started with Ruby and MongoDB
MongoDB
 
The Ruby/mongoDB ecosystem
Harold Giménez
 
MongoDB at RubyEnRails 2009
Mike Dirolf
 
Ruby Development and MongoMapper (John Nunemaker)
MongoSF
 
Rails Loves MongoDB
BillWatts
 
MongoDB
Steve Klabnik
 
MongoDB at FrozenRails
Mike Dirolf
 
MongoDB at ZPUGDC
Mike Dirolf
 
Mongo db
Antonio Terreno
 
Rails with MongoDB - RORLab 47th
Eugene Park
 
Tame Accidental Complexity with Ruby and MongoMapper
Giordano Scalzo
 
MongoMapper lightning talk
Kerry Buckley
 
Mongo db eveningschemadesign
MongoDB APAC
 
MongoDB at RuPy
Mike Dirolf
 
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
Ad

Mongoid in the real world