Simple MongoDB design for Rails apps
-
Upload
sergio-santos -
Category
Technology
-
view
2.607 -
download
2
description
Transcript of Simple MongoDB design for Rails apps
![Page 1: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/1.jpg)
Simple MongoDB design for Web (Rails) apps
Sérgio Santos@sdsantos
Improve Coimbra
![Page 2: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/2.jpg)
Introduction / Disclamer
![Page 3: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/3.jpg)
Bundlr MongoDB stats
MongoDB from the start
~ 3½ years of development
~ 6 GB of data
~ 6 million documents
Cloud hosted on MongoHQ
![Page 4: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/4.jpg)
Burocracia MongoDB stats~ 3 weekends of development
~ 300MB of data
~ 721 documents
~ 400KB per document
Cloud hosted on MongoLab
![Page 5: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/5.jpg)
Why MongoDB?
![Page 6: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/6.jpg)
MongoDB 101
![Page 7: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/7.jpg)
Documents
![Page 8: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/8.jpg)
Collections
![Page 9: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/9.jpg)
References
![Page 10: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/10.jpg)
Embeds
![Page 11: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/11.jpg)
Rails MongoDB ORMs
![Page 12: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/12.jpg)
Mongoid vs MongoMapper
![Page 13: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/13.jpg)
Mongoid
![Page 14: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/14.jpg)
Modelsclass Person include Mongoid::Documentend
person = Person.newperson[:name] = 'Sérgio'person[:age] = 26person.save
![Page 15: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/15.jpg)
Modelsclass Person include Mongoid::Document field :name, type: String field :age, type: Integerend
person = Person.newperson.name = 'Sérgio'person.age = 26person.save
![Page 16: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/16.jpg)
Persistenceperson = Person.create(name: 'Sérgio', age: 26)
person.update_attributes(name: 'Sérgio Santos')
person.touch
person.delete
person.destroy
person.rename(:name, :first_name)
![Page 17: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/17.jpg)
Queryingperson = Person.find("4baa56f1230048567300485c")
people = Person.where(age: 18)
people = Person.where(name: 'Sérgio').not(age: 26)
Person.count
Person.all.avg(:age)
-- INSERT MAP/REDUCE CLEVER EXAMPLE HERE --
![Page 18: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/18.jpg)
Referencesclass Band include Mongoid::Document has_many :membersend
class Member include Mongoid::Document field :name, type: String belongs_to :bandend
![Page 19: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/19.jpg)
References# The parent band document.{ "_id" : ObjectId("4d3ed089fb60ab534684b7e9")}
# The child member document. { "_id" : ObjectId("4d3ed089fb60ab534684b7f1"), "name" : "Matt Berninger" "band_id" : ObjectId("4d3ed089fb60ab534684b7e9")}
![Page 20: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/20.jpg)
Embedsclass Band include Mongoid::Document embeds_many :albumsend
class Album include Mongoid::Document field :name, type: String embedded_in :bandend
![Page 21: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/21.jpg)
Embeds{ "_id" : ObjectId("4d3ed089fb60ab534684b7e9"), "albums" : [ { "_id" : ObjectId("4d3ed089fb60ab534684b7e0"), "name" : "Boxer", } ]}
![Page 22: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/22.jpg)
Extras
timestamps
identity map & cache
paranoia
versioning
![Page 23: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/23.jpg)
Cloud Hosting
![Page 24: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/24.jpg)
![Page 25: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/25.jpg)
The Nice Stuff
![Page 26: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/26.jpg)
No need to create the collection
class Band include Mongoid::Documentend
![Page 27: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/27.jpg)
Store anythingclass ParamsStore include Mongoid::Documentend
class ApplicationController < ActionController::Base before_filter do Stuff.create(params) endend
![Page 28: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/28.jpg)
Store long stuffclass Website include Mongoid::Document field :html, type: String field :css, type: String field :javascript, type: String embeds_many :imagesend
class Image include Mongoid::Document field :size, type: Integer field :file, type: Moped::BSON::Binary embedded_in :websiteend
![Page 29: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/29.jpg)
Geoclass Person include Mongoid::Document field :location, :type => Array index( { location: Mongo::GEO2D }, { min: -180, max: 180 } )end
coimbra_location = [20, -8]Person.geo_near(coimbra_location).spherical
![Page 30: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/30.jpg)
Full text searchclass Document include Mongoid::Document field :content, :type => String index( {:content => 'text'}, {:default_language => 'portuguese’} )end
Document.text_search('IKEA')
![Page 31: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/31.jpg)
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
![Page 32: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/32.jpg)
( Bundlr ) Examples
![Page 33: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/33.jpg)
Settingsclass User include Mongoid::Document embeds_one :settingsend
class Settings include Mongoid:Document field :receive_emails, type: Boolean field :theme, type: Symbol embedded_in :userend
![Page 34: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/34.jpg)
Oauthsclass 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 :userend
![Page 35: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/35.jpg)
Activity Streamclass Activity include Mongoid::Document include Mongoid::Timestamps
field :verb, :type => Symbol field :actor, :type => Hash field :object, :type => Hash field :target, :type => Hash field :receivers, :type => Array embeds_many :likesend
![Page 36: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/36.jpg)
Relationshipsclass User include Mongoid::Document has_and_belongs_to_many :followers, :class_name => "User", :inverse_of => :followed_usersend
{ _id: ObjectId("4d541f6823380f670d000008"), follower_ids: [ ObjectId("4d4ea8ec86d56d2907000003"), ObjectId("50fe17c226c1b7000200018a") ],}
Need smarter relationships? Embed them!
![Page 37: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/37.jpg)
Other examples
Model hierarchy
Migrations
Changing schema?
No right way of doing things… is all about
context!
![Page 38: Simple MongoDB design for Rails apps](https://reader036.fdocuments.us/reader036/viewer/2022081602/55515a92b4c905a8768b4bf0/html5/thumbnails/38.jpg)
Wrapping up…