SOMETHING SOMETHING
                     MONGO
                           Introduction to MongoDB
            Persisting d...
1
                                                                            2
                                          ...
Saturday 1 May 2010
WHAT IS IT?


    • Document-based               data store

    • JSON-style            data (BSON)

    • Built         ...
SCALING



    • Has             built in High Availability (Replication and Failover)

    • Has             built in Aut...
SPEED


    • GridFS          as document store

    • In-place        updates

    • Indexes




Saturday 1 May 2010
QUERYING


    • rich        syntax

    • sorting           (order by), skipping (limit)

    • field            selection...
SCHEMALESS

    • No         fixed set of fields when storing an document

         • title: “foo”, author: “bar”

         ...
USE...

                      YES                          NO

    • (most) Websites          • Transactions

    • Cachin...
START USING IT
    • MongoDB           in google: www.mongodb.org - Quickstart

         • make       a directory

       ...
START USING IT




Saturday 1 May 2010
START USING IT




Saturday 1 May 2010
QUERYING

           > db.arrrrcamp.save({talk: "MongoDB", who: "Bernard"})
           > db.arrrrcamp.find()
           {
 ...
MORE QUERYING

           > db.arrrrcamp.find({who: "Bernard")
           {
             "_id" : ObjectId("4b681a1ecf1bcfea...
MORE QUERYING




Saturday 1 May 2010
Saturday 1 May 2010
Saturday 1 May 2010
gem install mongo_mapper




Saturday 1 May 2010
gem install mongo_mapper
                        gem install mongo_ext




Saturday 1 May 2010
require 'mongo_mapper'
                      MongoMapper.database = ‘arrrrcamp’




Saturday 1 May 2010
class Presenter
                        include MongoMapper::Document
                      end




Saturday 1 May 2010
class Presenter
                        include MongoMapper::Document

                       key :name, String
          ...
presenter = Presenter.new({
                        :name => “Bernard”,
                        :confirmed => true,
       ...
EMBEDDED DOCUMENT


                      include MongoMapper::EmbeddedDocument




Saturday 1 May 2010
ACTIVERECORD, ANYONE?




Saturday 1 May 2010
MONGOMAPPER HAS


    • validates_*

    • before_save        / after_update / ...

    • has_many         / belongs_to

 ...
STORING CUSTOM
                              DATATYPES

    • to_mongo     - serialize your own data to a known mongo-
   ...
class Keywords
     def initialize(array)
        @words = array
     end

       def to_a
         @words
       end

   ...
class Presenter
                   include MongoMapper::Document
                   key :keywords, Keywords
              ...
DYNAMIC DATA




Saturday 1 May 2010
INHERITANCE

                class Page                 class BlogPost < Page
                  key :permalink, String    ...
MODULAR
                      module Authentication
                       def self.included(base)
                       ...
MULTISITE WEBSHOP
                          EXAMPLE
                         per instance data gathering




Saturday 1 Ma...
WHAT TO BUILD


    • Ordering            system - multiple stores: shoes, tv’s, mobile phones

    • One             syst...
SOLUTION



    • metaprogramming: dynamically     generate a class for each site

    • dynamically     add the fields to ...
SITE-CLASS


    • Can             be a Mongo-document

    • Can             be an ActiveRecord object

    • Can        ...
ORDER CLASS: MM

    class Order
     include MongoMapper::Document
     key :order_id, String
    end



Saturday 1 May 2...
FACTORY-PATTERN
    class Order

        class << self
          def for_site(site)
            klass = Class.new(self)
  ...
DYNAMIC DOCUMENT
                          BUILDING

    class Order
      def order_fields=(fields)
         fields.each do ...
class Order
      include MongoMapper::Document
      key :order_id, String

        class << self
          def for_site(...
TRY IT OUT


         current_site.order_fields
         #=> [“field1”, “field2”]
         @order = Order.for_site(current_si...
Saturday 1 May 2010
Saturday 1 May 2010
Saturday 1 May 2010
Q&A




Saturday 1 May 2010
Upcoming SlideShare
Loading in …5
×

Persisting dynamic data with mongodb and mongomapper

4,498 views

Published on

Introduction to MongoDB
Persisting dynamic data

Published in: Technology

Persisting dynamic data with mongodb and mongomapper

  1. 1. SOMETHING SOMETHING MONGO Introduction to MongoDB Persisting dynamic data with MongoDB, MongoMapper & metaprogramming Saturday 1 May 2010
  2. 2. 1 2 3 4 5 6 7 • Bernard Grymonpon 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 • Openminds BVBA • Rails Hosting & Rails-shop ribbit • @wonko_be - @openminds_be Top sites deserve maximum uptime openminds.be tailored hosting solutions Saturday 1 May 2010
  3. 3. Saturday 1 May 2010
  4. 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. 5. SCALING • Has built in High Availability (Replication and Failover) • Has built in Auto-sharding Saturday 1 May 2010
  6. 6. SPEED • GridFS as document store • In-place updates • Indexes Saturday 1 May 2010
  7. 7. QUERYING • rich syntax • sorting (order by), skipping (limit) • field selection • mapreduce (group by) Saturday 1 May 2010
  8. 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. 9. USE... YES NO • (most) Websites • Transactions • Caching • Lots of joins • Scalabilty • Fixed datasets (science,...) Saturday 1 May 2010
  10. 10. START USING IT • MongoDB in google: www.mongodb.org - Quickstart • make a directory • download mongodb • unpack Saturday 1 May 2010
  11. 11. START USING IT Saturday 1 May 2010
  12. 12. START USING IT Saturday 1 May 2010
  13. 13. QUERYING > db.arrrrcamp.save({talk: "MongoDB", who: "Bernard"}) > db.arrrrcamp.find() { "_id" : ObjectId("4b681a1ecf1bcfea49f4b2ae"), "talk" : "MongoDB", "who" : "Bernard" } Saturday 1 May 2010
  14. 14. MORE QUERYING > db.arrrrcamp.find({who: "Bernard") { "_id" : ObjectId("4b681a1ecf1bcfea49f4b2ae"), "talk" : "MongoDB", "who" : "Bernard" } Saturday 1 May 2010
  15. 15. MORE QUERYING Saturday 1 May 2010
  16. 16. Saturday 1 May 2010
  17. 17. Saturday 1 May 2010
  18. 18. gem install mongo_mapper Saturday 1 May 2010
  19. 19. gem install mongo_mapper gem install mongo_ext Saturday 1 May 2010
  20. 20. require 'mongo_mapper' MongoMapper.database = ‘arrrrcamp’ Saturday 1 May 2010
  21. 21. class Presenter include MongoMapper::Document end Saturday 1 May 2010
  22. 22. class Presenter include MongoMapper::Document key :name, String key :confirmed, Boolean key :keywords, Array key :presentation, Presentation end Saturday 1 May 2010
  23. 23. presenter = Presenter.new({ :name => “Bernard”, :confirmed => true, :keywords => [“Openminds”, “geek”] }) presenter.save presenter.destroy Saturday 1 May 2010
  24. 24. EMBEDDED DOCUMENT include MongoMapper::EmbeddedDocument Saturday 1 May 2010
  25. 25. ACTIVERECORD, ANYONE? Saturday 1 May 2010
  26. 26. MONGOMAPPER HAS • validates_* • before_save / after_update / ... • has_many / belongs_to • timestamps! Saturday 1 May 2010
  27. 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. 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. 29. class Presenter include MongoMapper::Document key :keywords, Keywords end Saturday 1 May 2010
  30. 30. DYNAMIC DATA Saturday 1 May 2010
  31. 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. 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. 33. MULTISITE WEBSHOP EXAMPLE per instance data gathering Saturday 1 May 2010
  34. 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. 35. SOLUTION • metaprogramming: dynamically generate a class for each site • dynamically add the fields to the mongomapper Saturday 1 May 2010
  36. 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. 37. ORDER CLASS: MM class Order include MongoMapper::Document key :order_id, String end Saturday 1 May 2010
  38. 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. 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. 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. 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
  42. 42. Saturday 1 May 2010
  43. 43. Saturday 1 May 2010
  44. 44. Saturday 1 May 2010
  45. 45. Q&A Saturday 1 May 2010

×