An Introduction

 to Mongodb

 and Mongoid

What is it?


A database really good at storing documents
NoSQL 
Differs from traditional DB's
Does not have relationships
JSON like structures with dynamic schema

Why?

Dont we have enough DBs already?


Designed to be
  • Fast
  • Scalable
  • Flexible

Interacting with Mongo


Enter the console
 #mongo


Javascript Interpreter
Lets check out a demo

Mongoid

Ruby ORM for mongodb
API familiar to ruby developers
Similar to ActiveRecord and Datamapper

Mongo Community


It has Solid documentation at 
http://mongoid.org

An active irc at #mongoid

Also a Mailing List at http://groups.google.com/group/mongoid

Railties

Generators for models
Hooks into ActiveModel

Validations


validates_associated :episodes
validates :episodes, associated: true
validates_format_of :title, with: /\A\w+\Z/ validates :title, format: { with: /\A\w+\Z/ }

Mass Assignment


 # Set the field values in the document.
Person.new(first_name: "Jean-Baptiste", middle_name: "Emmanuel")
person.attributes = { first_name: "Jean-Baptiste", middle_name: "Emmanuel" }
person.write_attributes(
  first_name: "Jean-Baptiste",
  middle_name: "Emmanuel"
)

Serialization


 class Foo
  include Mongoid::Document
  field :bar
end
 
foo = Foo.new(:bar => "baz")
foo.to_json # => { bar: "baz" }

Scopes


class User
  scope :active, :status => 'active'
  scope :over, :lambda { |age| where(:age.gt => age) }
end

User.where(:name => /^J/).active.over(40)

callbacks


 class Article
  include Mongoid::Document
  field :name, type: String
  field :body, type: String
  field :slug, type: String

  before_create :send_message

  after_save do |document|
    # Handle callback here.
  end

  protected
  def send_message
    # Message sending code here.
  end
end

Timestamps


 class Person
  include Mongoid::Document
  include Mongoid::Timestamps
end
//This adds basic behavior for created_at and updated_at fields.

Framework Agnostic


Works with Sinatra and Padrino
 
Alongside other ORMS like ActiveRecord and Datamapper
 

Powerful Query Language


Familar to ActiveRecord users
AREL like syntax

User.where(:name => "John")
User.all(:age.lt => 24, :age.gt => 18)
User.where(:title.in => %w(Ms Mrs))

Named Scopes


class User
  scope :active, :status => 'active'
  scope :over, :lambda { |age| where(:age.gt => age) }
end
User.where(:name => /^J/).active.over(40)

GEOSPACIAL INDEXES


 class Spot
  include Mongoid::Document

  field :name, :type => String
  field :latlng, :type => Array

  index [[:latlng, Mongo::GEO2D]]
end
Ruby Spot.create( :name => "Majoran Distillery", :latlng => [41.910973,-87.635544] )

Spot.where(:latlng.near => majoran.latlng) #=> [“Majoran Distillery”, ”Cibo”, “Westpac” ]
Bar.where(:likes.gt => 100).geo_near([ 50, 12 ]).spherical

Apps thats USe Mongodb


Locomotive CMS
 Shapado a Q&A style site
Graylog2 for sorting server logs.

Sample App Demo

TV Guide

...


MongoDB and Mongoid

By Kieran Andrews

MongoDB and Mongoid

  • 1,282
Loading comments...

More from Kieran Andrews