Rails + mongo db

Post on 15-May-2015

331 views 0 download

Transcript of Rails + mongo db

Rails + MongoDB = <3

Sérgio Santos@sdsantos

Improve Coimbra

Introduction / Disclamer

Bundlr MongoDB stats

MongoDB from the start

~ 3½ years of development

~ 6 GB of data

~ 6 million documents

Cloud hosted on MongoHQ

MongoDB 101

Documents

Collections

References

Embeds

Rails MongoDB ORMs

Mongoid vs MongoMapper

Mongoid

Modelsclass Person include Mongoid::Documentend

person = Person.newperson[:name] = 'Sérgio'person[:age] = 26person.save

Modelsclass Person include Mongoid::Document field :name, type: String field :age, type: Integerend

person = Person.newperson.name = 'Sérgio'person.age = 26person.save

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)

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 --

Referencesclass Band include Mongoid::Document has_many :membersend

class Member include Mongoid::Document field :name, type: String belongs_to :bandend

References# The parent band document.{ "_id" : ObjectId("4d3ed089fb60ab534684b7e9")}

# The child member document. { "_id" : ObjectId("4d3ed089fb60ab534684b7f1"), "name" : "Matt Berninger" "band_id" : ObjectId("4d3ed089fb60ab534684b7e9")}

Embedsclass Band include Mongoid::Document embeds_many :albumsend

class Album include Mongoid::Document field :name, type: String embedded_in :bandend

Embeds{ "_id" : ObjectId("4d3ed089fb60ab534684b7e9"), "albums" : [ { "_id" : ObjectId("4d3ed089fb60ab534684b7e0"), "name" : "Boxer", } ]}

Extrasidentity map & cache

paranoia

versioning

timestamps

geo

full text search

Cloud Hosting

Wrapping up…

Rails + MongoDB = <3Sérgio Santos

@sdsantosme@sergiosantos.info

We’re hiring!nourishcare.co.uk/careers/