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
Models class Person include Mongoid::Document end person = Person.new person[:name] = 'Sérgio' person[:age] = 26 person.save
Models class Person include Mongoid::Document field :name, type: String field :age, type: Integer end person = Person.new person.name = 'Sérgio' person.age = 26 person.save
Persistence person = 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)
Querying person = 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 --
References class Band include Mongoid::Document has_many :members end class Member include Mongoid::Document field :name, type: String belongs_to :band end
References # The parent band document. { "_id" : ObjectId("4d3ed089fb60ab534684b7e9") } # The child member document. { "_id" : ObjectId("4d3ed089fb60ab534684b7f1"), "name" : "Matt Berninger" "band_id" : ObjectId("4d3ed089fb60ab534684b7e9") }
Embeds class Band include Mongoid::Document embeds_many :albums end class Album include Mongoid::Document field :name, type: String embedded_in :band end
Embeds { "_id" : ObjectId("4d3ed089fb60ab534684b7e9"), "albums" : [ { "_id" : ObjectId("4d3ed089fb60ab534684b7e0"), "name" : "Boxer", } ] }
Extras identity map & cache paranoia versioning timestamps geo full text search
Cloud Hosting
Wrapping up…
Rails + MongoDB = <3 Sérgio Santos @sdsantos me@sergiosantos.info We’re hiring! nourishcare.co.uk/careers/

Rails + mongo db