![Page 1: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/1.jpg)
with Mongoid
*MongoDB + RoR
![Page 2: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/2.jpg)
*Agenda
*NoSQL
*What is MongoDB?
*Comparison – MongoDB vs. RDBMS
*What is Mongoid?
*Live MongoDB + Rails examples
*Pros and cons
*Summary
![Page 3: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/3.jpg)
*My experience
*Been working with Mongoid for a few months
*MongoDB 1.6
*Mongoid 2.0.0.beta.20
*Not yet on production
![Page 4: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/4.jpg)
*NoSQL movement
![Page 5: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/5.jpg)
*So what exactly is MongoDB?
*Document oriented Database
*Supports indexes
*Rich querying
*Auto-sharding
*GridFS
*BSON format
![Page 6: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/6.jpg)
*MongoDB vs. SQL
Document Documentreference
Documentreference
![Page 7: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/7.jpg)
*MongoDB vs. SQL
Document
Embedded document
Embedded document
Documentreference
Documentreference
![Page 8: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/8.jpg)
*MongoDB vs. SQL
Document
Embedded document
Embedded document
Documentreference
Documentreference
![Page 9: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/9.jpg)
*What is ?
*Think of ActiveRecord for MongoDB
*Mapping Ruby objects to MongoDB documents and embedded documents
*Supports Rails3
*Named scopes, rich criteria API
*Also some problems…
![Page 10: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/10.jpg)
*How to use ?
class Building include Mongoid::Document
include Mongoid::Timestamps
embeds_many :floors
field :name
field :occupied, :type => Boolean, :default => false
field :area, :type => Integer
end
![Page 11: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/11.jpg)
*How to use ?
class Level include Mongoid::Document
embedded_in :building, :inverse_of => :levels
embeds_many :rooms
field :type
validate :type, :presence => true, :uniqueness => {:scope => :city}
end
![Page 12: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/12.jpg)
*How to use ?
Building.first(:conditions => {:name.in => [“Dom”,”Stodoła”]})
Building.levels.where(:type => “poddasze”)
Building.find(:all, :conditions => {:area.gt => 100})
![Page 13: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/13.jpg)
*How to use ?
Callbacks:
after_save, before_save, before_validation, after_create, etc.
Inheritance:
class House < Building
end
house = House.new; house._type # => “house”
Building.all # [house]
![Page 14: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/14.jpg)
*Pros and cons of
Pros Cons
• Gives the power of MongoDB
o collectionso Good for heavy-write
applicationso Auto-sharding• Very similar to
ActiveRecord• Dynamic development
• Many bugs• Documentation for 2.0 is
not complete• Not all gems are
compatible• Slow references
(embedding over references!)
![Page 15: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/15.jpg)
*Examples of
Roflcopter.pl
![Page 16: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/16.jpg)
*Summary
Summary in a few months (when our app goes production)
![Page 17: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/17.jpg)
*Summary
But…
![Page 18: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/18.jpg)
*Summary
…don’t wait and try it yourself!
![Page 20: Grzegorz Witek - MongoDB + RoR, Mongoid (PRUG 1.0)](https://reader036.vdocument.in/reader036/viewer/2022082921/55625277d8b42aa02d8b50cc/html5/thumbnails/20.jpg)
*Kolejne spotkanie
*28 kwietnia – 19:00
*Prezentacje?
http://prug.pl