Mongodb on Ruby And Rails (froscon 2010)

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
1 of 183

More Related Content

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

More from jan_mindmatters(11)

realtime audio on ze web @ hhjsrealtime audio on ze web @ hhjs
realtime audio on ze web @ hhjs
jan_mindmatters1.2K views
Facebook mit Rails und FacebookerFacebook mit Rails und Facebooker
Facebook mit Rails und Facebooker
jan_mindmatters799 views
HAML / SASS and COMPASSHAML / SASS and COMPASS
HAML / SASS and COMPASS
jan_mindmatters1.2K views
Merb. Rails in anders.Merb. Rails in anders.
Merb. Rails in anders.
jan_mindmatters919 views
Lehmanns Rails ErweiternLehmanns Rails Erweitern
Lehmanns Rails Erweitern
jan_mindmatters991 views
Rails i18n - Railskonferenz 2007Rails i18n - Railskonferenz 2007
Rails i18n - Railskonferenz 2007
jan_mindmatters1.2K views

Recently uploaded(20)

ThroughputThroughput
Throughput
Moisés Armani Ramírez31 views
ChatGPT and AI for Web DevelopersChatGPT and AI for Web Developers
ChatGPT and AI for Web Developers
Maximiliano Firtman161 views
Java Platform Approach 1.0 - Picnic MeetupJava Platform Approach 1.0 - Picnic Meetup
Java Platform Approach 1.0 - Picnic Meetup
Rick Ossendrijver24 views

Mongodb on Ruby And Rails (froscon 2010)