4. WHAT IS IT?
• Document-based data store
• JSON-style data (BSON)
• Built for speed (searching)
• Massive amounts of data (humongous)
Saturday 1 May 2010
5. SCALING
• Has built in High Availability (Replication and Failover)
• Has built in Auto-sharding
Saturday 1 May 2010
6. SPEED
• GridFS as document store
• In-place updates
• Indexes
Saturday 1 May 2010
8. SCHEMALESS
• No fixed set of fields when storing an document
• title: “foo”, author: “bar”
• title: “arrrr”, author: “captain”, when: “today”
• We get empty fields when selecting non-existing data
• find will return “when” as empty when selecting all data
Saturday 1 May 2010
9. USE...
YES NO
• (most) Websites • Transactions
• Caching • Lots of joins
• Scalabilty • Fixed datasets (science,...)
Saturday 1 May 2010
10. START USING IT
• MongoDB in google: www.mongodb.org - Quickstart
• make a directory
• download mongodb
• unpack
Saturday 1 May 2010
27. STORING CUSTOM
DATATYPES
• to_mongo - serialize your own data to a known mongo-
storable data structure (string, array,...)
• from_mongo - unserialize your data and return an object (of
“self ”)
• both are class methods (!)
Saturday 1 May 2010
28. class Keywords
def initialize(array)
@words = array
end
def to_a
@words
end
class << self
def to_mongo(value)
value.is_a?(Keywords) ? value.to_a : value
end
def from_mongo(value)
value ? self.new(value) : nil
end
end
end
Saturday 1 May 2010
29. class Presenter
include MongoMapper::Document
key :keywords, Keywords
end
Saturday 1 May 2010
31. INHERITANCE
class Page class BlogPost < Page
key :permalink, String key :content, String
key :title, String has_many :comments
end end
class Redirect < Page
key :destination, String
end
Saturday 1 May 2010
32. MODULAR
module Authentication
def self.included(base)
base.key :password, String
base.key :salt, String
base.key :login, String
end
end
class Person
include MongoMapper::Document
include Authentication
end
Saturday 1 May 2010
33. MULTISITE WEBSHOP
EXAMPLE
per instance data gathering
Saturday 1 May 2010
34. WHAT TO BUILD
• Ordering system - multiple stores: shoes, tv’s, mobile phones
• One system
• Each shop has its own fields to require for an order
Saturday 1 May 2010
35. SOLUTION
• metaprogramming: dynamically generate a class for each site
• dynamically add the fields to the mongomapper
Saturday 1 May 2010
36. SITE-CLASS
• Can be a Mongo-document
• Can be an ActiveRecord object
• Can be a (Rails 3) ActiveModel object (REST?)
• Has to “respond_to? :id” and “respond_to? :order_fields”
Saturday 1 May 2010
37. ORDER CLASS: MM
class Order
include MongoMapper::Document
key :order_id, String
end
Saturday 1 May 2010
38. FACTORY-PATTERN
class Order
class << self
def for_site(site)
klass = Class.new(self)
klass.order_fields = site.order_fields
Object.const_set("OrderForSite#{site.id}", klass)
klass
end
end
end
Saturday 1 May 2010
39. DYNAMIC DOCUMENT
BUILDING
class Order
def order_fields=(fields)
fields.each do |field|
key field, String
end
end
end
Saturday 1 May 2010
40. class Order
include MongoMapper::Document
key :order_id, String
class << self
def for_site(site)
klass = Class.new(self)
klass.order_fields = site.order_fields
Object.const_set("OrderForSite#{site.id}", klass)
klass
end
def order_fields=(fields)
fields.each do |field|
key field, String
end
end
end
end
Saturday 1 May 2010
41. TRY IT OUT
current_site.order_fields
#=> [“field1”, “field2”]
@order = Order.for_site(current_site).new
#=> #<OrderForSite2172858380 field1: nil, field2: nil,
order_id: nil>
Saturday 1 May 2010