Mongodb on Ruby And Rails (froscon 2010)

Post on 11-May-2015

2.906 views 0 download

Tags:

description

Slides to my talk at the FrosCon in St. Augustin

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

document database

NoSQLincluded!

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

Sonntag, 22. August 2010

rich data structure

Sonntag, 22. August 2010

{ "_id" : ObjectId("4c00245062610475a005afcd"), "address" : "Bernstorffstr. 174\n22767 Hamburg\nDE", "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

Prim

ary

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 testswitched 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$set$unset$push$pushAll

$addToSet$pop$pull$pullAll$

Sonntag, 22. August 2010

getting a whole collection

Sonntag, 22. August 2010

@db['quotes'].find.each do |row| puts row.inspectend

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}); }); }ENDreduce = <<-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.filenamefile.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.readend

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.rbFile.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 = @connectionMongoMapper.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, Arrayend

class Address include MongoMapper::Document key :street key :city key :country key :zipend

Sonntag, 22. August 2010

person = Person.firstaddress = 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 => Integerend

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

class Address include Mongoid::Document field :street field :city field :country field :zipend

Sonntag, 22. August 2010

person = Person.firstaddress = 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 --masterormaster = true # mongodb.conf

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

slave = truesource = 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.

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

‣ http://www.mongodb.org/‣ http://www.mongoid.org/‣ http://github.com/jnunemaker/mongo_mapper‣ http://github.com/halfbyte/mongo_ruby_examples

Sonntag, 22. August 2010