Tame accidental complexity       Introduction to NoSQL with MongoMapperGiordano Scalzo
I’m not here to talk about performance
I’m not here to talk about scalability
but I’m here to talk about simplicity
Rails has been a first step
Anatomy of a Rails Application
Anatomy of a Rails Applicationview
Anatomy of a Rails Applicationview       controller
Anatomy of a Rails Applicationview       controller      model
Different languageshtml+css   view        controller       model
Different languageshtml+css        oop   view        controller       model
Different languageshtml+css        oop               sql   view        controller       model
Impedance mismatchhtml+css        oop               sql   view         controller      model
Origin of SqlSQLFrom Wikipedia, the free encyclopediaSQL (...), is a database computer languagedesigned for managing data ...
Origin of SqlSQLFrom Wikipedia, the free encyclopediaSQL (...), is a database computer languagedesigned for managing data ...
We need persistent objects!
We need persistent objects!class User  def initialize(username, password)    @username = username    @password = password ...
We need persistent objects!   {       username: "giordano",       password: "123"   }
ActiveRecord tries its best
We need something different
Persistence
Persistenceclass User  include MongoMapper::Documentend
Persistenceclass User  include MongoMapper::Documentenduser = User.create({   :username => "giordano",   :password => "123...
Persistenceclass User  include MongoMapper::Documentenduser = User.create({   :username => "giordano",   :password => "123...
Persistence{    "_id"=>BSON::ObjectId(4d643a274d8ff683dd000001),    "username"=>"giordano",    "password"=>"123"}
Types
Typesclass User  include MongoMapper::Document  key :username, String  key :password , Stringend
Built-in TypesArray, Binary, Boolean, Date,Float, Hash, Integer, Nil,ObjectId, Set, String, Time
Custom Typesclass DowncasedString  def self.to_mongo(value)    value.nil? ? nil : value.to_s.downcase  end  def self.from_...
Custom Typesclass User  include MongoMapper::Document  key :username, String  key :password , String  key :email, Downcase...
Custom Typesuser = User.newuser.username = "giordano"user.password = "123"user.email = "Giordano.Scalzo@CleanCode.it"user....
Custom Types{ "_id"=>BSON::ObjectId(4d6442d94d8ff684d3000001), "username"=>"giordano", "password"=>"123", "email"=>"giorda...
Embedded Documents
Embedded Documentsclass Task  include MongoMapper::EmbeddedDocument  key :description, String  key :pomodori, Integer  key...
Embedded Documentsclass User  include MongoMapper::Document  key :username, String  key :password , String  key :email, Do...
Embedded Documentsuser.tasks << Task.new({   description: refactor server,   pomodori: 8,   is_done: false})user.tasks << ...
Embedded Documents{"_id"=>BSON::ObjectId(4d6575e84d8ff692e6000001),  "username"=>"giordano", "password"=>"123",  "email"=>...
Embedded Documents{"_id"=>BSON::ObjectId(4d6575e84d8ff692e6000001),  "username"=>"giordano", "password"=>"123",  "email"=>...
Documentsclass Task  include MongoMapper::Document  key :description, String  key :pomodori, Integer  key :is_done, Boolea...
Documentsp User.all.last.to_mongo{ "_id"=>BSON::ObjectId(4d657e924d8ff6949c000001), "username"=>"giordano", "password"=>"1...
Documentsp User.all.last.tasks[#<Task _id:    BSON::ObjectId(4d65822b4d8ff69542000002),    description: "refactor server",...
Validations & Callbacks
Validations & Callbacksclass User  include MongoMapper::Document  key :username, String,  validates_presence_of :username ...
Validations & Callbacksclass User  include MongoMapper::Document  key :username, String, :required => true  key :password,...
Validations & Callbacksvalidates_presence_ofvalidates_length_ofvalidates_format_ofvalidates_numericality_ofvalidates_accep...
Validations & Callbacksbefore_save                   after_savebefore_create                 after_createbefore_update    ...
Validations & Callbacksforked in current gem 0.8.6using Rails3 ActiveModel inRails3 branch just merged
What about querying?
What about querying?query = User.where(:last_name.exists => true,                   :created_at.gte => from_date,         ...
What about querying?query = User.where(:last_name.exists => true,                   :created_at.gte => from_date,         ...
What about querying?query = User.where(:last_name.exists => true)            .where(:created_at.gte => from_date)         ...
What about plugins?
What about plugins?Accessible              ModifiersAssociations            PaginationCaching                 PersistenceC...
Itʼs just a beginning
http://mongomapper.com/documentation/
http://mongoid.org/
giordano.scalzo@cleancode.it@giordanoscalzowww.slideshare.net/giordanogithub.com/gscalzo
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapper
Upcoming SlideShare
Loading in...5
×

Tame Accidental Complexity with Ruby and MongoMapper

3,049

Published on

Gentle introduction to MongoDb with Ruby and MongoMapper

2 Comments
2 Likes
Statistics
Notes
  • Great presentation.

    Many thanks,

    Atish
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • ESCUELA COLOMBIANA DE CARRERAS INDUSTRIALES


    JOHANA ROCIO BERNAL NOCUA
    CODIGO 2010181422
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
3,049
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
2
Likes
2
Embeds 0
No embeds

No notes for slide

Tame Accidental Complexity with Ruby and MongoMapper

  1. 1. Tame accidental complexity Introduction to NoSQL with MongoMapperGiordano Scalzo
  2. 2. I’m not here to talk about performance
  3. 3. I’m not here to talk about scalability
  4. 4. but I’m here to talk about simplicity
  5. 5. Rails has been a first step
  6. 6. Anatomy of a Rails Application
  7. 7. Anatomy of a Rails Applicationview
  8. 8. Anatomy of a Rails Applicationview controller
  9. 9. Anatomy of a Rails Applicationview controller model
  10. 10. Different languageshtml+css view controller model
  11. 11. Different languageshtml+css oop view controller model
  12. 12. Different languageshtml+css oop sql view controller model
  13. 13. Impedance mismatchhtml+css oop sql view controller model
  14. 14. Origin of SqlSQLFrom Wikipedia, the free encyclopediaSQL (...), is a database computer languagedesigned for managing data in relationaldatabase management systems (RDBMS)
  15. 15. Origin of SqlSQLFrom Wikipedia, the free encyclopediaSQL (...), is a database computer languagedesigned for managing data in relationaldatabase management systems (RDBMS)
  16. 16. We need persistent objects!
  17. 17. We need persistent objects!class User def initialize(username, password) @username = username @password = password endend
  18. 18. We need persistent objects! { username: "giordano", password: "123" }
  19. 19. ActiveRecord tries its best
  20. 20. We need something different
  21. 21. Persistence
  22. 22. Persistenceclass User include MongoMapper::Documentend
  23. 23. Persistenceclass User include MongoMapper::Documentenduser = User.create({ :username => "giordano", :password => "123"})user.save
  24. 24. Persistenceclass User include MongoMapper::Documentenduser = User.create({ :username => "giordano", :password => "123"})user.saveputs User.all.last.to_mongo
  25. 25. Persistence{ "_id"=>BSON::ObjectId(4d643a274d8ff683dd000001), "username"=>"giordano", "password"=>"123"}
  26. 26. Types
  27. 27. Typesclass User include MongoMapper::Document key :username, String key :password , Stringend
  28. 28. Built-in TypesArray, Binary, Boolean, Date,Float, Hash, Integer, Nil,ObjectId, Set, String, Time
  29. 29. Custom Typesclass DowncasedString def self.to_mongo(value) value.nil? ? nil : value.to_s.downcase end def self.from_mongo(value) value.nil? ? nil : value.to_s.downcase endend
  30. 30. Custom Typesclass User include MongoMapper::Document key :username, String key :password , String key :email, DowncasedStringend
  31. 31. Custom Typesuser = User.newuser.username = "giordano"user.password = "123"user.email = "Giordano.Scalzo@CleanCode.it"user.saveputs User.all.last.to_mongo
  32. 32. Custom Types{ "_id"=>BSON::ObjectId(4d6442d94d8ff684d3000001), "username"=>"giordano", "password"=>"123", "email"=>"giordano.scalzo@cleancode.it"}
  33. 33. Embedded Documents
  34. 34. Embedded Documentsclass Task include MongoMapper::EmbeddedDocument key :description, String key :pomodori, Integer key :is_done, Booleanend
  35. 35. Embedded Documentsclass User include MongoMapper::Document key :username, String key :password , String key :email, DowncasedString many :tasksend
  36. 36. Embedded Documentsuser.tasks << Task.new({ description: refactor server, pomodori: 8, is_done: false})user.tasks << Task.new({ description: timer sound, pomodori: 2, is_done: false})
  37. 37. Embedded Documents{"_id"=>BSON::ObjectId(4d6575e84d8ff692e6000001), "username"=>"giordano", "password"=>"123", "email"=>"giordano.scalzo@cleancode.it", "tasks"=>[{ "_id"=>BSON::ObjectId(4d6575e84d8ff692e6000002), "description"=>"refactor server", "pomodori"=>8, "is_done"=>false }, { "_id"=>BSON::ObjectId(4d6575e84d8ff692e6000003), "description"=>"timer sound", "pomodori"=>2, "is_done"=>false }]}
  38. 38. Embedded Documents{"_id"=>BSON::ObjectId(4d6575e84d8ff692e6000001), "username"=>"giordano", "password"=>"123", "email"=>"giordano.scalzo@cleancode.it", "tasks"=>[{ "_id"=>BSON::ObjectId(4d6575e84d8ff692e6000002), "description"=>"refactor server", "pomodori"=>8, "is_done"=>false }, { "_id"=>BSON::ObjectId(4d6575e84d8ff692e6000003), "description"=>"timer sound", "pomodori"=>2, "is_done"=>false }]}
  39. 39. Documentsclass Task include MongoMapper::Document key :description, String key :pomodori, Integer key :is_done, Booleanend
  40. 40. Documentsp User.all.last.to_mongo{ "_id"=>BSON::ObjectId(4d657e924d8ff6949c000001), "username"=>"giordano", "password"=>"123", "email"=>"giordano.scalzo@cleancode.it"}
  41. 41. Documentsp User.all.last.tasks[#<Task _id: BSON::ObjectId(4d65822b4d8ff69542000002), description: "refactor server", pomodori: 8, is_done: false, user_id: BSON::ObjectId(4d65822b4d8ff69542000001) >, #<Task _id: BSON::ObjectId(4d65822b4d8ff69542000003), description: "timer sound", pomodori: 2, is_done: false, user_id: BSON::ObjectId(4d65822b4d8ff69542000001) >]
  42. 42. Validations & Callbacks
  43. 43. Validations & Callbacksclass User include MongoMapper::Document key :username, String, validates_presence_of :username key :password, String validates_presence_of :passwordend
  44. 44. Validations & Callbacksclass User include MongoMapper::Document key :username, String, :required => true key :password, String, :required => trueend
  45. 45. Validations & Callbacksvalidates_presence_ofvalidates_length_ofvalidates_format_ofvalidates_numericality_ofvalidates_acceptance_ofvalidates_confirmation_ofvalidates_inclusion_ofvalidates_exclusion_of
  46. 46. Validations & Callbacksbefore_save after_savebefore_create after_createbefore_update after_updatebefore_validation after_validationbefore_validation_on_create after_validation_on_createbefore_validation_on_update after_validation_on_updatebefore_destroy after_destroyvalidate_on_create validate_on_updatevalidate
  47. 47. Validations & Callbacksforked in current gem 0.8.6using Rails3 ActiveModel inRails3 branch just merged
  48. 48. What about querying?
  49. 49. What about querying?query = User.where(:last_name.exists => true, :created_at.gte => from_date, :created_at.lt => Time.now) Plucky: ActiveRecord-like language .skip(0).limit(5)query.all
  50. 50. What about querying?query = User.where(:last_name.exists => true, :created_at.gte => from_date, :created_at.lt => Time.now) Plucky: ActiveRecord-like language .skip(0).limit(5)#<Plucky::Query created_at: { "$gte"=>"1", "$lt"=>2011-02-24 10:54:36 UTC}, last_name: {"$exists"=>true}, limit: 5, skip: 0>
  51. 51. What about querying?query = User.where(:last_name.exists => true) .where(:created_at.gte => from_date) .where(:created_at.lt => Time.now) Plucky: ActiveRecord-like language .skip(0).limit(5)#<Plucky::Query created_at: { "$gte"=>"1", "$lt"=>2011-02-24 10:54:36 UTC}, last_name: {"$exists"=>true}, limit: 5, skip: 0>
  52. 52. What about plugins?
  53. 53. What about plugins?Accessible ModifiersAssociations PaginationCaching PersistenceCallbacks ProtectedClone QueryingDirty RailsDocument SafeDynamic Querying SingleEmbeddedDocument CollectionEquality InheritanceIdentityMap ScopesIndexes SerializationInspect TimestampsKeys UserstampsLogger Validations
  54. 54. Itʼs just a beginning
  55. 55. http://mongomapper.com/documentation/
  56. 56. http://mongoid.org/
  57. 57. giordano.scalzo@cleancode.it@giordanoscalzowww.slideshare.net/giordanogithub.com/gscalzo
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×