SlideShare a Scribd company logo
on rails

                froscon 2010, st. augustin.

                jan krutisch <jan@krutisch.de>
                http://jan.krutisch.de/



Sonntag, 22. August 2010
mongodb wtf? lol!?




Sonntag, 22. August 2010
document database




Sonntag, 22. August 2010
NoS
                                       QL
                                inclu
                                      ded!



                document database




Sonntag, 22. August 2010
10gen




Sonntag, 22. August 2010
open source



               http://github.com/mongodb/mongo
Sonntag, 22. August 2010
id   title   descr   pos_lat pos_lng




Sonntag, 22. August 2010
no fixed schema
                no migrations



Sonntag, 22. August 2010
rich data structure




Sonntag, 22. August 2010
{
                    "_id" : ObjectId("4c00245062610475a005afcd"),
                    "address" : "Bernstorffstr. 174n22767 HamburgnDE",
                    "description" : null,
                    "position" : {
                      "lat" : 53.5600912,
                      "lng" : 9.9596977
                    },
                    "tags" : [
                      "hausarzt",
                      "naturheilverfahren",
                      "akupunktur",
                      "allgemeinmedizin"
                    ],
                    "title" : "Dr. med. Lilo Eisenbarth",
                    "loxicon_id" : 808261
                }




Sonntag, 22. August 2010
Sonntag, 22. August 2010
Sonntag, 22. August 2010
                           ✗
Sonntag, 22. August 2010
BSON




Sonntag, 22. August 2010
BInary Serialized jsON



  http://bsonspec.org/
Sonntag, 22. August 2010
Lightweight




Sonntag, 22. August 2010
Traversable




Sonntag, 22. August 2010
Efficient




Sonntag, 22. August 2010
Wire




Sonntag, 22. August 2010
Storage




Sonntag, 22. August 2010
rich queries




Sonntag, 22. August 2010
conceptually close to SQL




Sonntag, 22. August 2010
easy to grasp




Sonntag, 22. August 2010
flexible




Sonntag, 22. August 2010
language integration




Sonntag, 22. August 2010
on top: map/reduce




Sonntag, 22. August 2010
Scaling




Sonntag, 22. August 2010
Master/Slave replication




Sonntag, 22. August 2010
Replica Sets (1.6)




Sonntag, 22. August 2010
Primary




                           Member             Member




Sonntag, 22. August 2010
Primary

                           Member             Primary




Sonntag, 22. August 2010
Member




                           Member            Primary




Sonntag, 22. August 2010
Autosharding (1.6)




Sonntag, 22. August 2010
Sonntag, 22. August 2010
Durability




Sonntag, 22. August 2010
No single server
                durability!



Sonntag, 22. August 2010
fsyncs every 60s
                (configurable)



Sonntag, 22. August 2010
Use Replication!




Sonntag, 22. August 2010
Use write propagation
                locking



Sonntag, 22. August 2010
Single Server Durability
                planned for 1.8



Sonntag, 22. August 2010
mongo console




Sonntag, 22. August 2010
$ mongo




Sonntag, 22. August 2010
> use test
                switched to db test

                db.quotes.save({
                  text: "You can observe a lot just by watching.",
                  from: "Yogi Berra", created_at: new Date()
                });

                db.quotes.save({
                  text: "Silence is one of the hardest arguments to refute.",
                  from: "Josh Billings", created_at: new Date()
                });




Sonntag, 22. August 2010
Indexing




Sonntag, 22. August 2010
Same concept as with
                SQL databases



Sonntag, 22. August 2010
You want them




Sonntag, 22. August 2010
Same concept as with
                SQL databases



Sonntag, 22. August 2010
Sort order




Sonntag, 22. August 2010
Unique




Sonntag, 22. August 2010
Compound




Sonntag, 22. August 2010
Geospatial




Sonntag, 22. August 2010
map/reduce




Sonntag, 22. August 2010
we can haz it, too




Sonntag, 22. August 2010
function() {
                      this.tags.forEach(function(z) {
                        emit(z, {count: 1});
                      });
                    }



                    function(key, values) {
                      var total = 0;
                      values.forEach(function(v) { total += v });
                      return {count: total}
                    }




Sonntag, 22. August 2010
(it‘s not fast...)




Sonntag, 22. August 2010
security




Sonntag, 22. August 2010
simple user/password
                auth



Sonntag, 22. August 2010
per database




Sonntag, 22. August 2010
read only is possible




Sonntag, 22. August 2010
one more thing




Sonntag, 22. August 2010
GridFS




Sonntag, 22. August 2010
Binary fields in BSON
                < 4MB



Sonntag, 22. August 2010
GridFS saves files in
                chunks



Sonntag, 22. August 2010
I‘m in u‘r rubies,
                querying teh MongoDB!



Sonntag, 22. August 2010
core driver




Sonntag, 22. August 2010
mongo / bson_ext




Sonntag, 22. August 2010
ODMs / Libs




Sonntag, 22. August 2010
mongo_mapper




Sonntag, 22. August 2010
mongoid




Sonntag, 22. August 2010
Find examples here:
                http://github.com/halfbyte/mongo_ruby_examples




Sonntag, 22. August 2010
Basic driver usage




Sonntag, 22. August 2010
init




Sonntag, 22. August 2010
require 'mongo'

                @connection = Mongo::Connection.new
                @db = @connection.db("test")




Sonntag, 22. August 2010
insert/upsert




Sonntag, 22. August 2010
doc = {
                  :text => "You can observe a lot just by watching.",
                  :from => "Yogi Berra",
                  :created_at => Time.now
                }
                @db['quotes'].insert(doc)




Sonntag, 22. August 2010
doc = @db['quotes'].find_one(id)

                doc[:from] = "Yogi Berra, famous baseball player"

                @db['quotes'].save(doc)




Sonntag, 22. August 2010
atomic updates




Sonntag, 22. August 2010
@db['quotes'].update(
                  {"from" => "Yogi Berra"},
                  {"$inc" => {"reads" => 1 } }
                )




Sonntag, 22. August 2010
@db['quotes'].update(
                  {"from" => "Yogi Berra"},
                  {"$inc" => {"reads" => 1 } }
                )




Sonntag, 22. August 2010
$inc       $addToSet
                $set       $pop
                $unset     $pull
                $push      $pullAll
                $pushAll   $


Sonntag, 22. August 2010
getting a whole collection




Sonntag, 22. August 2010
@db['quotes'].find.each do |row|
                  puts row.inspect
                end




Sonntag, 22. August 2010
exact query




Sonntag, 22. August 2010
@db['quotes'].find(:from => "Yogi Berra")




Sonntag, 22. August 2010
more queries




Sonntag, 22. August 2010
100.times do |i|
                  db['numbers'].insert({"i" => i})
                end




Sonntag, 22. August 2010
db['numbers'].find("i" => {"$lt" => 2})




Sonntag, 22. August 2010
$lt        <
                $gt        >
                $lte       <=
                $gte       >=
                $ne        !=

Sonntag, 22. August 2010
@db['people'].find(:tags => {"$in" => ['cool']})




Sonntag, 22. August 2010
obj = {
                  "_id"=>BSON::ObjectID('4c706af16261040680000369'),
                  "name"=>"Vernon Kreiger",
                  "address"=>{
                    "street"=>"536 Haleigh Locks",
                    "city"=>"Port Kiannahaven",
                    "zip"=>"80730-0214",
                    "country"=>"Fakistan"
                  },
                  "tags"=>["cool", "weird"]
                }




Sonntag, 22. August 2010
obj = {
                  "_id"=>BSON::ObjectID('4c706af16261040680000369'),
                  "name"=>"Vernon Kreiger",
                  "address"=>{
                    "street"=>"536 Haleigh Locks",
                    "city"=>"Port Kiannahaven",
                    "zip"=>"80730-0214",
                    "country"=>"Fakistan"
                  },
                  "tags"=>["cool", "weird"]
                }




Sonntag, 22. August 2010
$in        IN (2,3,4)
                $nin       NOT IN
                $all       [2,3] ~ [1,2,3]


Sonntag, 22. August 2010
$mod       yah, RLY
                $size      okay
                $exists    NOT NULL
                $type      huh?


Sonntag, 22. August 2010
@db['people'].find("address.city" => /haven/)




Sonntag, 22. August 2010
@db['people'].find("address.city" => /haven/)




Sonntag, 22. August 2010
Sorting




Sonntag, 22. August 2010
@db['people'].find().sort("address.street")




Sonntag, 22. August 2010
@db['people'].find().sort("address.street")




Sonntag, 22. August 2010
Pagination




Sonntag, 22. August 2010
@db['numbers'].find.sort("i").limit(10)




Sonntag, 22. August 2010
@db['numbers'].find.sort("i").limit(10).skip(50)




Sonntag, 22. August 2010
Counting




Sonntag, 22. August 2010
@db['numbers'].find.count




Sonntag, 22. August 2010
Distinct




Sonntag, 22. August 2010
@db['people'].distinct('tags').inspect




Sonntag, 22. August 2010
Group




Sonntag, 22. August 2010
Poor mans map/reduce




Sonntag, 22. August 2010
Map / Reduce




Sonntag, 22. August 2010
map = <<-END
                  function() {
                    this.tags.forEach(function(z) {
                      emit(z, {count: 1});
                    });
                  }
                END
                reduce = <<-END
                  function(key, values) {
                    var total = 0;
                    values.forEach(function(v) { total += v.count });
                    return {count: total}
                  }
                END

                collection = @db['people'].map_reduce(
                  map, reduce
                )




Sonntag, 22. August 2010
Indexes




Sonntag, 22. August 2010
db['people'].create_index("tags")

                db['people'].drop_index("tags_1")

                db['people'].index_information




Sonntag, 22. August 2010
GridFS usage




Sonntag, 22. August 2010
grid = Mongo::Grid.new(@db)

                id = grid.put("You can put Strings in here",
                  :filename => 'test.txt')

                file = grid.get(id)

                file.filename
                file.read

                grid.delete(id)

                grid.put(
                  File.open("/Users/jankrutisch/Dropbox/Photos/IMGP8989.jpg")
                )




Sonntag, 22. August 2010
fs = Mongo::GridFileSystem.new(db)

                fs.open("test.txt", "w") do |f|
                  f.write "You can put stuff in here"
                end

                fs.open("test.txt", "r") do |f|
                  puts f.read
                end

                fs.delete("test.txt")




Sonntag, 22. August 2010
Capped collections




Sonntag, 22. August 2010
@db.create_collection('capped_numbers',
                  :capped => true,
                  :max => 50
                )


                @db.create_collection('capped_numbers',
                  :capped => true,
                  :size => 1024 * 64
                )




Sonntag, 22. August 2010
ODMs




Sonntag, 22. August 2010
mongo_mapper




Sonntag, 22. August 2010
John Nunemaker
                @jnunemaker



Sonntag, 22. August 2010
is in production




Sonntag, 22. August 2010
documentation?




Sonntag, 22. August 2010
Sonntag, 22. August 2010
how to




Sonntag, 22. August 2010
rails initializer




Sonntag, 22. August 2010
# config/initializers/mongo_mapper.rb
                File.open(File.join(Rails.root, 'config/mongodb.yml'), 'r') do |f|
                  @settings = YAML.load(f)[Rails.env]
                end
                MongoMapper.connection = Mongo::Connection.from_uri(@settings["connection"]) if @settings["connection"]

                MongoMapper.database = @settings["database"]




Sonntag, 22. August 2010
a simple example




Sonntag, 22. August 2010
MongoMapper.connection = @connection
                MongoMapper.database = "test"

                class Quote
                  include MongoMapper::Document
                  key :from
                  key :text
                  key :views, Integer
                  timestamps!
                end




Sonntag, 22. August 2010
finders




Sonntag, 22. August 2010
Quote.where(:from => 'Yogi Berra').all


                Quote.where(:from => 'Yogi Berra').limit(5).sort(:from.desc).all




Sonntag, 22. August 2010
embedded docs




Sonntag, 22. August 2010
class Person
                  include MongoMapper::Document
                  key :name
                  one :address
                  key :tags, Array
                end

                class Address
                  include MongoMapper::Document
                  key :street
                  key :city
                  key :country
                  key :zip
                end




Sonntag, 22. August 2010
person = Person.first
                address = Person.first.address




Sonntag, 22. August 2010
scopes




Sonntag, 22. August 2010
class Person
                  scope :tagged, lambda { |tag| where(:tags.in => [tag]) }
                end

                puts Person.tagged('cool').first.inspect




Sonntag, 22. August 2010
new website coming soon




Sonntag, 22. August 2010
mongoid




Sonntag, 22. August 2010
Durran Jordan
                (of Hashrocket)



Sonntag, 22. August 2010
Two major versions




Sonntag, 22. August 2010
1.x (1.9.x) targeting
                Rails 2.3.x



Sonntag, 22. August 2010
2.x (2.0beta) targeting
                Rails 3.0



Sonntag, 22. August 2010
Good documentation




Sonntag, 22. August 2010
Sonntag, 22. August 2010
rails initializer




Sonntag, 22. August 2010
File.open(File.join(RAILS_ROOT, 'config/mongodb.yml'), 'r') do |f|
                  @settings = YAML.load(f)[RAILS_ENV]
                end

                Mongoid::Config.instance.from_hash(@settings)




Sonntag, 22. August 2010
a simple example




Sonntag, 22. August 2010
class Quote
                  include Mongoid::Document
                  include Mongoid::Timestamps
                  field :from
                  field :text
                  field :views, :type => Integer
                end




Sonntag, 22. August 2010
finders




Sonntag, 22. August 2010
Quote.where(:from => 'Yogi Berra').all


                Quote.where(:from => 'Yogi Berra').limit(5).order_by(:from.desc).all




Sonntag, 22. August 2010
embedded docs




Sonntag, 22. August 2010
class Person
                  include Mongoid::Document
                  field :name
                  embeds_one :address
                  field :tags, :type => Array
                end

                class Address
                  include Mongoid::Document
                  field :street
                  field :city
                  field :country
                  field :zip
                end




Sonntag, 22. August 2010
person = Person.first
                address = Person.first.address




Sonntag, 22. August 2010
scopes




Sonntag, 22. August 2010
class Person
                  scope :tagged, lambda { |tag| where(:tags.in => [tag]) }
                end

                puts Person.tagged('cool').first.inspect




Sonntag, 22. August 2010
More features




Sonntag, 22. August 2010
atomic updates




Sonntag, 22. August 2010
mongoid tries to be
                clever



Sonntag, 22. August 2010
(using the „dirty“ flags)




Sonntag, 22. August 2010
(it‘s probably better to
                bypass the ODM
                sometimes)


Sonntag, 22. August 2010
GridFS




Sonntag, 22. August 2010
external libraries for
                both



Sonntag, 22. August 2010
mongo_mapper > grip




Sonntag, 22. August 2010
mongoid > mongoid_grid




Sonntag, 22. August 2010
Other noteworthy
                libraries



Sonntag, 22. August 2010
Candy




Sonntag, 22. August 2010
Candy




Sonntag, 22. August 2010
mongodoc



      http://github.com/leshill/mongodoc

Sonntag, 22. August 2010
mongo-record



      http://github.com/mongodb/mongo-record
Sonntag, 22. August 2010
mongomodel



        http://github.com/spohlenz/mongomodel
Sonntag, 22. August 2010
mongo_queue



            http://github.com/Skiz/mongo_queue
Sonntag, 22. August 2010
resque-mongo



  http://github.com/ctrochalakis/resque-mongo
Sonntag, 22. August 2010
my account




Sonntag, 22. August 2010
Installation was easy




Sonntag, 22. August 2010
(when on right platform)




Sonntag, 22. August 2010
setting up replication




Sonntag, 22. August 2010
$ mongod --master
                or
                master = true # mongodb.conf




                $ mongod --slave --source
                slaveserver.example.com

                slave = true
                source = slaveserver.example.com




Sonntag, 22. August 2010
(see example on github)




Sonntag, 22. August 2010
OpLog size!




Sonntag, 22. August 2010
„security“




Sonntag, 22. August 2010
memory usage?




Sonntag, 22. August 2010
limits?




Sonntag, 22. August 2010
stability?




Sonntag, 22. August 2010
I




Sonntag, 22. August 2010
thanks for listening.
                ‣     http://www.mongodb.org/
                ‣     http://www.mongoid.org/
                ‣     http://github.com/jnunemaker/mongo_mapper
                ‣     http://github.com/halfbyte/mongo_ruby_examples

                ‣     jan@krutisch.de
                ‣     http://jan.krutisch.de/
                ‣     http://github.com/halfbyte/
                ‣     http://twitter.com/halfbyte
                ‣     http://www.mindmatters.de/


Sonntag, 22. August 2010

More Related Content

PDF
MongoDB & Mongomapper 4 real
PDF
Bookmarklet型(云端)应用的前端架构
PDF
Mongodb railscamphh
PDF
Developing Plugins on OpenVBX at Greater San Francisco Bay Area LAMP Group
PDF
Groovy Introduction at Javaforum 2010
PDF
An Overview of HTML5 Storage
PDF
State of GeoTools 2012
PDF
Railsrumble railscamphh 2010
MongoDB & Mongomapper 4 real
Bookmarklet型(云端)应用的前端架构
Mongodb railscamphh
Developing Plugins on OpenVBX at Greater San Francisco Bay Area LAMP Group
Groovy Introduction at Javaforum 2010
An Overview of HTML5 Storage
State of GeoTools 2012
Railsrumble railscamphh 2010

Similar to Mongodb on Ruby And Rails (froscon 2010) (13)

PDF
Mongo db
PDF
MongoDB on Rails (and Ruby)
PDF
Intro To MongoDB
PDF
AtlasCamp 2010: Needs more jQuery - Using advanced Javascript in Atlassian Pl...
PDF
AtlasCamp 2010: Needs more jQuery - Using advanced Javascript in Atlassian Pl...
PDF
AtlasCamp 2010: Needs more jQuery - Using advanced Javascript in Atlassian Pl...
PDF
Upgrading to Rails 3
PDF
iPhone for .NET Developers
PDF
Developing in HTML5: Widgetbox & Sencha Touch
PDF
dojo is bizarro jQuery
PDF
Is these a bug
PDF
Continuous Integration Testing for Plone Using Hudson
PDF
Campus Party 2010
Mongo db
MongoDB on Rails (and Ruby)
Intro To MongoDB
AtlasCamp 2010: Needs more jQuery - Using advanced Javascript in Atlassian Pl...
AtlasCamp 2010: Needs more jQuery - Using advanced Javascript in Atlassian Pl...
AtlasCamp 2010: Needs more jQuery - Using advanced Javascript in Atlassian Pl...
Upgrading to Rails 3
iPhone for .NET Developers
Developing in HTML5: Widgetbox & Sencha Touch
dojo is bizarro jQuery
Is these a bug
Continuous Integration Testing for Plone Using Hudson
Campus Party 2010
Ad

More from jan_mindmatters (11)

PDF
Ruby for Artists and Tinkerers. A non-presentation.
PDF
realtime audio on ze web @ hhjs
PDF
10 fun projects to improve your coding skills
PDF
Open Source Hardware - Of makers and tinkerers
PDF
Liebe Dein Frontend wie Dich selbst! HAML & SASS & COMPASS & less
PDF
Facebook mit Rails und Facebooker
PDF
Show the frontend some love - HAML, SASS and COMPASS
PDF
HAML / SASS and COMPASS
PDF
Merb. Rails in anders.
PDF
Lehmanns Rails Erweitern
PDF
Rails i18n - Railskonferenz 2007
Ruby for Artists and Tinkerers. A non-presentation.
realtime audio on ze web @ hhjs
10 fun projects to improve your coding skills
Open Source Hardware - Of makers and tinkerers
Liebe Dein Frontend wie Dich selbst! HAML & SASS & COMPASS & less
Facebook mit Rails und Facebooker
Show the frontend some love - HAML, SASS and COMPASS
HAML / SASS and COMPASS
Merb. Rails in anders.
Lehmanns Rails Erweitern
Rails i18n - Railskonferenz 2007
Ad

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
Teaching material agriculture food technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation theory and applications.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Machine learning based COVID-19 study performance prediction
PDF
KodekX | Application Modernization Development
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
PPTX
MYSQL Presentation for SQL database connectivity
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Teaching material agriculture food technology
The AUB Centre for AI in Media Proposal.docx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation theory and applications.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectral efficient network and resource selection model in 5G networks
Understanding_Digital_Forensics_Presentation.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Approach and Philosophy of On baking technology
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Unlocking AI with Model Context Protocol (MCP)
Machine learning based COVID-19 study performance prediction
KodekX | Application Modernization Development
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
MYSQL Presentation for SQL database connectivity

Mongodb on Ruby And Rails (froscon 2010)