Simple MongoDB design for Rails apps

  • Published on
    12-May-2015

  • View
    2.602

  • Download
    2

DESCRIPTION

Introduction to MongoDB, Rails Mongoid ORM and some data modelling examples.

Transcript

<ul><li>1.Simple MongoDB design for Web (Rails) apps Srgio Santos @sdsantos Improve Coimbra </li></ul> <p>2. Introduction / Disclamer 3. Bundlr MongoDB stats MongoDB from the start ~ 3 years of development ~ 6 GB of data ~ 6 million documents Cloud hosted on MongoHQ 4. Burocracia MongoDB stats ~ 3 weekends of development ~ 300MB of data ~ 721 documents ~ 400KB per document Cloud hosted on MongoLab 5. Why MongoDB? 6. MongoDB 101 7. Documents 8. Collections 9. References 10. Embeds 11. Rails MongoDB ORMs 12. Mongoid vs MongoMapper 13. Mongoid 14. Models class Person include Mongoid::Document end person = Person.new person[:name] = 'Srgio' person[:age] = 26 person.save 15. Models class Person include Mongoid::Document field :name, type: String field :age, type: Integer end person = Person.new person.name = 'Srgio' person.age = 26 person.save 16. Persistence person = Person.create(name: 'Srgio', age: 26) person.update_attributes(name: 'Srgio Santos') person.touch person.delete person.destroy person.rename(:name, :first_name) 17. Querying person = Person.find("4baa56f1230048567300485c") people = Person.where(age: 18) people = Person.where(name: 'Srgio').not(age: 26) Person.count Person.all.avg(:age) -- INSERT MAP/REDUCE CLEVER EXAMPLE HERE -- 18. References class Band include Mongoid::Document has_many :members end class Member include Mongoid::Document field :name, type: String belongs_to :band end 19. References # The parent band document. { "_id" : ObjectId("4d3ed089fb60ab534684b7e9") } # The child member document. { "_id" : ObjectId("4d3ed089fb60ab534684b7f1"), "name" : "Matt Berninger" "band_id" : ObjectId("4d3ed089fb60ab534684b7e9") } 20. Embeds class Band include Mongoid::Document embeds_many :albums end class Album include Mongoid::Document field :name, type: String embedded_in :band end 21. Embeds { "_id" : ObjectId("4d3ed089fb60ab534684b7e9"), "albums" : [ { "_id" : ObjectId("4d3ed089fb60ab534684b7e0"), "name" : "Boxer", } ] } 22. Extras timestamps identity map &amp; cache paranoia versioning 23. Cloud Hosting 24. The Nice Stuff 25. No need to create the collection class Band include Mongoid::Document end 26. Store anything class ParamsStore include Mongoid::Document end class ApplicationController &lt; ActionController::Base before_filter do Stuff.create(params) end end 27. Store long stuff class Website include Mongoid::Document field :html, type: String field :css, type: String field :javascript, type: String embeds_many :images end class Image include Mongoid::Document field :size, type: Integer field :file, type: Moped::BSON::Binary embedded_in :website end 28. Geo class Person include Mongoid::Document field :location, :type =&gt; Array index( { location: Mongo::GEO2D }, { min: -180, max: 180 } ) end coimbra_location = [20, -8] Person.geo_near(coimbra_location).spherical 29. Full text search class Document include Mongoid::Document field :content, :type =&gt; String index( {:content =&gt; 'text'}, {:default_language =&gt; 'portuguese} ) end Document.text_search('IKEA') 30. Caveats (not so nice) No joins Impact of key length No integrity checks for references Document size limit of 16MB Nesting depth limit of 100 31. ( Bundlr ) Examples 32. Settings class User include Mongoid::Document embeds_one :settings end class Settings include Mongoid:Document field :receive_emails, type: Boolean field :theme, type: Symbol embedded_in :user end 33. Oauths class User include Mongoid::Document embeds_many :oauths index [["oauths.provider", Mongo::ASCENDING], ["oauths.uid", Mongo::ASCENDING]] end class Oauth include Mongoid:Document field :provider, type: String field :token, type: String field :secret, type: String field :uid, type: String embedded_in :user end 34. Activity Stream class Activity include Mongoid::Document include Mongoid::Timestamps field :verb, :type =&gt; Symbol field :actor, :type =&gt; Hash field :object, :type =&gt; Hash field :target, :type =&gt; Hash field :receivers, :type =&gt; Array embeds_many :likes end 35. Relationships class User include Mongoid::Document has_and_belongs_to_many :followers, :class_name =&gt; "User", :inverse_of =&gt; :followed_users end { _id: ObjectId("4d541f6823380f670d000008"), follower_ids: [ ObjectId("4d4ea8ec86d56d2907000003"), ObjectId("50fe17c226c1b7000200018a") ], } Need smarter relationships? Embed them! 36. Other examples Model hierarchy Migrations Changing schema? No right way of doing things is all about context! 37. Wrapping up 38. Srgio Santos @sdsantos me@sergiosantos.info Simple MongoDB design for Web (Rails) apps Improve Coimbra </p>