Download - Mongodb railscamphh

Transcript
Page 1: Mongodb railscamphh

on rails

railscamp hamburg, 2010

jan krutisch <[email protected]>http://jan.krutisch.de/

Samstag, 23. Oktober 2010

Page 2: Mongodb railscamphh

mongodb wtf? lol!?

Samstag, 23. Oktober 2010

Page 3: Mongodb railscamphh

document database

Samstag, 23. Oktober 2010

Page 4: Mongodb railscamphh

document database

NoSQLincluded!

Samstag, 23. Oktober 2010

Page 5: Mongodb railscamphh

10gen

Samstag, 23. Oktober 2010

Page 6: Mongodb railscamphh

open source

http://github.com/mongodb/mongo

Samstag, 23. Oktober 2010

Page 7: Mongodb railscamphh

id title descr pos_lat pos_lng

Samstag, 23. Oktober 2010

Page 8: Mongodb railscamphh

{ "_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 }

Samstag, 23. Oktober 2010

Page 9: Mongodb railscamphh

Samstag, 23. Oktober 2010

Page 10: Mongodb railscamphh

✗Samstag, 23. Oktober 2010

Page 11: Mongodb railscamphh

Samstag, 23. Oktober 2010

Page 12: Mongodb railscamphh

BSON

Samstag, 23. Oktober 2010

Page 13: Mongodb railscamphh

BInary Serialized jsON

http://bsonspec.org/Samstag, 23. Oktober 2010

Page 14: Mongodb railscamphh

Wire

Samstag, 23. Oktober 2010

Page 15: Mongodb railscamphh

Storage

Samstag, 23. Oktober 2010

Page 16: Mongodb railscamphh

rich queries

Samstag, 23. Oktober 2010

Page 17: Mongodb railscamphh

conceptually close to SQL

Samstag, 23. Oktober 2010

Page 18: Mongodb railscamphh

easy to grasp

Samstag, 23. Oktober 2010

Page 19: Mongodb railscamphh

flexible

Samstag, 23. Oktober 2010

Page 20: Mongodb railscamphh

language integration

Samstag, 23. Oktober 2010

Page 21: Mongodb railscamphh

on top: map/reduce

Samstag, 23. Oktober 2010

Page 22: Mongodb railscamphh

Scaling

Samstag, 23. Oktober 2010

Page 23: Mongodb railscamphh

Master/Slave replication

Samstag, 23. Oktober 2010

Page 24: Mongodb railscamphh

Replica Sets (1.6)

Samstag, 23. Oktober 2010

Page 25: Mongodb railscamphh

Primary

Member Member

Samstag, 23. Oktober 2010

Page 26: Mongodb railscamphh

Prim

ary

Member Primary

Samstag, 23. Oktober 2010

Page 27: Mongodb railscamphh

Member

Member Primary

Samstag, 23. Oktober 2010

Page 28: Mongodb railscamphh

Autosharding (1.6)

Samstag, 23. Oktober 2010

Page 29: Mongodb railscamphh

Samstag, 23. Oktober 2010

Page 30: Mongodb railscamphh

Durability

Samstag, 23. Oktober 2010

Page 31: Mongodb railscamphh

No single server durability!

Samstag, 23. Oktober 2010

Page 32: Mongodb railscamphh

fsyncs every 60s(configurable)

Samstag, 23. Oktober 2010

Page 33: Mongodb railscamphh

Use Replication!

Samstag, 23. Oktober 2010

Page 34: Mongodb railscamphh

Use write propagation locking

Samstag, 23. Oktober 2010

Page 35: Mongodb railscamphh

Single Server Durability planned for 1.8

Samstag, 23. Oktober 2010

Page 36: Mongodb railscamphh

mongo console

Samstag, 23. Oktober 2010

Page 37: Mongodb railscamphh

$ mongo

Samstag, 23. Oktober 2010

Page 38: Mongodb railscamphh

> 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() });

Samstag, 23. Oktober 2010

Page 39: Mongodb railscamphh

> 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() });

Samstag, 23. Oktober 2010

Page 40: Mongodb railscamphh

Indexing

Samstag, 23. Oktober 2010

Page 41: Mongodb railscamphh

Same concept as with SQL databases

Samstag, 23. Oktober 2010

Page 42: Mongodb railscamphh

You want them

Samstag, 23. Oktober 2010

Page 43: Mongodb railscamphh

Same concept as with SQL databases

Samstag, 23. Oktober 2010

Page 44: Mongodb railscamphh

Sort order

Samstag, 23. Oktober 2010

Page 45: Mongodb railscamphh

Unique

Samstag, 23. Oktober 2010

Page 46: Mongodb railscamphh

Compound

Samstag, 23. Oktober 2010

Page 47: Mongodb railscamphh

Geospatial

Samstag, 23. Oktober 2010

Page 48: Mongodb railscamphh

map/reduce

Samstag, 23. Oktober 2010

Page 49: Mongodb railscamphh

we can haz it, too

Samstag, 23. Oktober 2010

Page 50: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 51: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 52: Mongodb railscamphh

(it‘s not fast...)

Samstag, 23. Oktober 2010

Page 53: Mongodb railscamphh

security

Samstag, 23. Oktober 2010

Page 54: Mongodb railscamphh

simple user/password auth

Samstag, 23. Oktober 2010

Page 55: Mongodb railscamphh

per database

Samstag, 23. Oktober 2010

Page 56: Mongodb railscamphh

read only is possible

Samstag, 23. Oktober 2010

Page 57: Mongodb railscamphh

one more thing

Samstag, 23. Oktober 2010

Page 58: Mongodb railscamphh

GridFS

Samstag, 23. Oktober 2010

Page 59: Mongodb railscamphh

Binary fields in BSON < 4MB

Samstag, 23. Oktober 2010

Page 60: Mongodb railscamphh

GridFS saves files in chunks

Samstag, 23. Oktober 2010

Page 61: Mongodb railscamphh

I‘m in u‘r rubies,querying teh MongoDB!

Samstag, 23. Oktober 2010

Page 62: Mongodb railscamphh

core driver

Samstag, 23. Oktober 2010

Page 63: Mongodb railscamphh

mongo / bson_ext

Samstag, 23. Oktober 2010

Page 64: Mongodb railscamphh

ODMs / Libs

Samstag, 23. Oktober 2010

Page 65: Mongodb railscamphh

mongo_mapper

Samstag, 23. Oktober 2010

Page 66: Mongodb railscamphh

mongoid

Samstag, 23. Oktober 2010

Page 67: Mongodb railscamphh

Find examples here:http://github.com/halfbyte/mongo_ruby_examples

Samstag, 23. Oktober 2010

Page 68: Mongodb railscamphh

Basic driver usage

Samstag, 23. Oktober 2010

Page 69: Mongodb railscamphh

init

Samstag, 23. Oktober 2010

Page 70: Mongodb railscamphh

require 'mongo'

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

Samstag, 23. Oktober 2010

Page 71: Mongodb railscamphh

@connection = Mongo::Connection.new( 'localhost', 27017, :pool_size => 5, :timeout => 20)

Samstag, 23. Oktober 2010

Page 72: Mongodb railscamphh

@connection = Mongo::Connection.from_uri( "mongodb://localhost:27017/test")

Samstag, 23. Oktober 2010

Page 73: Mongodb railscamphh

insert/upsert

Samstag, 23. Oktober 2010

Page 74: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 75: Mongodb railscamphh

doc = @db['quotes'].find_one(id)

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

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

Samstag, 23. Oktober 2010

Page 76: Mongodb railscamphh

atomic updates

Samstag, 23. Oktober 2010

Page 77: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 78: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 79: Mongodb railscamphh

$inc$set$unset$push$pushAll

$addToSet$pop$pull$pullAll$

Samstag, 23. Oktober 2010

Page 80: Mongodb railscamphh

getting a whole collection

Samstag, 23. Oktober 2010

Page 81: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 82: Mongodb railscamphh

exact query

Samstag, 23. Oktober 2010

Page 83: Mongodb railscamphh

@db['quotes'].find(:from => "Yogi Berra")

Samstag, 23. Oktober 2010

Page 84: Mongodb railscamphh

more queries

Samstag, 23. Oktober 2010

Page 85: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 86: Mongodb railscamphh

db['numbers'].find("i" => {"$lt" => 2})

Samstag, 23. Oktober 2010

Page 87: Mongodb railscamphh

$lt <$gt >$lte <=$gte >=$ne !=

Samstag, 23. Oktober 2010

Page 88: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 89: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 90: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 91: Mongodb railscamphh

$in IN (2,3,4)$nin NOT IN$all [2,3] ~ [1,2,3]

Samstag, 23. Oktober 2010

Page 92: Mongodb railscamphh

$mod yah, RLY$size okay$exists NOT NULL$type huh?

Samstag, 23. Oktober 2010

Page 93: Mongodb railscamphh

@db['people'].find("address.city" => /haven/)

Samstag, 23. Oktober 2010

Page 94: Mongodb railscamphh

@db['people'].find("address.city" => /haven/)

Samstag, 23. Oktober 2010

Page 95: Mongodb railscamphh

Sorting

Samstag, 23. Oktober 2010

Page 96: Mongodb railscamphh

@db['people'].find().sort("address.street")

Samstag, 23. Oktober 2010

Page 97: Mongodb railscamphh

@db['people'].find().sort("address.street")

Samstag, 23. Oktober 2010

Page 98: Mongodb railscamphh

Pagination

Samstag, 23. Oktober 2010

Page 99: Mongodb railscamphh

@db['numbers'].find.sort("i").limit(10)

Samstag, 23. Oktober 2010

Page 100: Mongodb railscamphh

@db['numbers'].find.sort("i").limit(10).skip(50)

Samstag, 23. Oktober 2010

Page 101: Mongodb railscamphh

Counting

Samstag, 23. Oktober 2010

Page 102: Mongodb railscamphh

@db['numbers'].find.count

Samstag, 23. Oktober 2010

Page 103: Mongodb railscamphh

Distinct

Samstag, 23. Oktober 2010

Page 104: Mongodb railscamphh

@db['people'].distinct('tags').inspect

Samstag, 23. Oktober 2010

Page 105: Mongodb railscamphh

Group

Samstag, 23. Oktober 2010

Page 106: Mongodb railscamphh

Poor mans map/reduce

Samstag, 23. Oktober 2010

Page 107: Mongodb railscamphh

@db['people'].group( ['created_at'], {}, {:tags => {}}, reduce, finalize)

Samstag, 23. Oktober 2010

Page 108: Mongodb railscamphh

@db['people'].group( ['created_at'], {}, {:tags => {}}, reduce, finalize)

Samstag, 23. Oktober 2010

Page 109: Mongodb railscamphh

@db['people'].group( ['created_at'], {}, {:tags => {}}, reduce, finalize)

Samstag, 23. Oktober 2010

Page 110: Mongodb railscamphh

@db['people'].group( ['created_at'], {}, {:tags => {}}, reduce, finalize)

Samstag, 23. Oktober 2010

Page 111: Mongodb railscamphh

@db['people'].group( ['created_at'], {}, {:tags => {}}, reduce, finalize)

Samstag, 23. Oktober 2010

Page 112: Mongodb railscamphh

function(doc, prev) { for(i in doc.tags) { if (doc.tags[i] in prev.tags) { prev.tags[doc.tags[i]]++ } else { prev.tags[doc.tags[i]] =1 } }}

Samstag, 23. Oktober 2010

Page 113: Mongodb railscamphh

{"created_at"=>2010-09-19 22:00:00 UTC, "tags"=>{"foo"=>11.0, "dumb"=>12.0, "stupid"=>7.0, "bar"=>7.0, "cool"=>14.0, "weird"=>17.0}}{"created_at"=>2010-09-20 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "stupid"=>5.0, "foo"=>10.0, "cool"=>8.0, "weird"=>9.0, "bar"=>15.0}}{"created_at"=>2010-09-22 22:00:00 UTC, "tags"=>{"weird"=>15.0, "bar"=>9.0, "stupid"=>17.0, "cool"=>11.0, "dumb"=>10.0, "foo"=>12.0}}{"created_at"=>2010-09-15 22:00:00 UTC, "tags"=>{"foo"=>11.0, "weird"=>7.0, "stupid"=>10.0, "cool"=>11.0, "bar"=>5.0, "dumb"=>8.0}}{"created_at"=>2010-09-25 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "weird"=>14.0, "cool"=>8.0, "foo"=>21.0, "bar"=>11.0, "stupid"=>13.0}}{"created_at"=>2010-09-28 22:00:00 UTC, "tags"=>{"cool"=>11.0, "dumb"=>16.0, "stupid"=>11.0, "weird"=>15.0, "foo"=>9.0, "bar"=>16.0}}{"created_at"=>2010-09-10 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>9.0, "bar"=>8.0, "cool"=>14.0, "stupid"=>11.0, "foo"=>11.0}}{"created_at"=>2010-09-03 22:00:00 UTC, "tags"=>{"weird"=>14.0, "dumb"=>15.0, "stupid"=>11.0, "foo"=>16.0, "bar"=>20.0, "cool"=>10.0}}{"created_at"=>2010-09-21 22:00:00 UTC, "tags"=>{"weird"=>15.0, "cool"=>14.0, "foo"=>13.0, "stupid"=>6.0, "bar"=>11.0, "dumb"=>9.0}}{"created_at"=>2010-09-23 22:00:00 UTC, "tags"=>{"weird"=>15.0, "stupid"=>15.0, "dumb"=>15.0, "foo"=>16.0, "cool"=>10.0, "bar"=>11.0}}{"created_at"=>2010-09-29 22:00:00 UTC, "tags"=>{"bar"=>9.0, "cool"=>14.0, "weird"=>16.0, "foo"=>8.0, "dumb"=>9.0, "stupid"=>12.0}}{"created_at"=>2010-09-27 22:00:00 UTC, "tags"=>{"cool"=>13.0, "dumb"=>10.0, "stupid"=>12.0, "bar"=>8.0, "foo"=>16.0, "weird"=>13.0}}{"created_at"=>2010-09-04 22:00:00 UTC, "tags"=>{"cool"=>11.0, "bar"=>9.0, "stupid"=>6.0, "weird"=>11.0, "dumb"=>8.0, "foo"=>11.0}}{"created_at"=>2010-09-08 22:00:00 UTC, "tags"=>{"stupid"=>12.0, "dumb"=>11.0, "cool"=>15.0, "foo"=>11.0, "bar"=>9.0, "weird"=>8.0}}{"created_at"=>2010-10-02 22:00:00 UTC, "tags"=>{"bar"=>8.0, "dumb"=>8.0, "cool"=>10.0, "foo"=>10.0, "stupid"=>8.0, "weird"=>6.0}}{"created_at"=>2010-09-24 22:00:00 UTC, "tags"=>{"foo"=>13.0, "bar"=>12.0, "stupid"=>15.0, "weird"=>17.0, "dumb"=>7.0, "cool"=>10.0}}{"created_at"=>2010-09-30 22:00:00 UTC, "tags"=>{"bar"=>10.0, "cool"=>6.0, "stupid"=>14.0, "weird"=>9.0, "dumb"=>12.0, "foo"=>19.0}}{"created_at"=>2010-09-05 22:00:00 UTC, "tags"=>{"dumb"=>12.0, "foo"=>19.0, "weird"=>8.0, "stupid"=>8.0, "bar"=>7.0, "cool"=>10.0}}{"created_at"=>2010-09-17 22:00:00 UTC, "tags"=>{"weird"=>13.0, "bar"=>14.0, "dumb"=>12.0, "foo"=>12.0, "stupid"=>10.0, "cool"=>9.0}}{"created_at"=>2010-09-18 22:00:00 UTC, "tags"=>{"dumb"=>8.0, "cool"=>11.0, "foo"=>6.0, "bar"=>12.0, "weird"=>7.0, "stupid"=>6.0}}{"created_at"=>2010-09-09 22:00:00 UTC, "tags"=>{"weird"=>11.0, "dumb"=>9.0, "foo"=>6.0, "bar"=>11.0, "cool"=>11.0, "stupid"=>6.0}}{"created_at"=>2010-09-13 22:00:00 UTC, "tags"=>{"dumb"=>19.0, "stupid"=>9.0, "weird"=>12.0, "cool"=>11.0, "bar"=>10.0, "foo"=>15.0}}{"created_at"=>2010-09-16 22:00:00 UTC, "tags"=>{"bar"=>6.0, "weird"=>8.0, "dumb"=>9.0, "cool"=>11.0, "stupid"=>17.0, "foo"=>15.0}}{"created_at"=>2010-09-11 22:00:00 UTC, "tags"=>{"foo"=>10.0, "weird"=>9.0, "bar"=>8.0, "cool"=>4.0, "dumb"=>8.0, "stupid"=>9.0}}{"created_at"=>2010-09-26 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>6.0, "stupid"=>15.0, "bar"=>10.0, "foo"=>13.0, "cool"=>15.0}}{"created_at"=>2010-10-01 22:00:00 UTC, "tags"=>{"cool"=>7.0, "weird"=>11.0, "stupid"=>11.0, "bar"=>14.0, "foo"=>12.0, "dumb"=>11.0}}{"created_at"=>2010-09-12 22:00:00 UTC, "tags"=>{"bar"=>7.0, "weird"=>12.0, "stupid"=>11.0, "cool"=>10.0, "foo"=>11.0, "dumb"=>9.0}}{"created_at"=>2010-09-14 22:00:00 UTC, "tags"=>{"dumb"=>8.0, "foo"=>15.0, "cool"=>15.0, "stupid"=>15.0, "bar"=>7.0, "weird"=>14.0}}{"created_at"=>2010-09-07 22:00:00 UTC, "tags"=>{"dumb"=>10.0, "cool"=>7.0, "foo"=>14.0, "weird"=>15.0, "bar"=>11.0, "stupid"=>7.0}}{"created_at"=>2010-09-06 22:00:00 UTC, "tags"=>{"dumb"=>7.0, "bar"=>11.0, "cool"=>16.0, "weird"=>14.0, "foo"=>12.0, "stupid"=>6.0}}

Samstag, 23. Oktober 2010

Page 114: Mongodb railscamphh

{"created_at"=>2010-09-19 22:00:00 UTC, "tags"=>{"foo"=>11.0, "dumb"=>12.0, "stupid"=>7.0, "bar"=>7.0, "cool"=>14.0, "weird"=>17.0}}{"created_at"=>2010-09-20 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "stupid"=>5.0, "foo"=>10.0, "cool"=>8.0, "weird"=>9.0, "bar"=>15.0}}{"created_at"=>2010-09-22 22:00:00 UTC, "tags"=>{"weird"=>15.0, "bar"=>9.0, "stupid"=>17.0, "cool"=>11.0, "dumb"=>10.0, "foo"=>12.0}}{"created_at"=>2010-09-15 22:00:00 UTC, "tags"=>{"foo"=>11.0, "weird"=>7.0, "stupid"=>10.0, "cool"=>11.0, "bar"=>5.0, "dumb"=>8.0}}{"created_at"=>2010-09-25 22:00:00 UTC, "tags"=>{"dumb"=>11.0, "weird"=>14.0, "cool"=>8.0, "foo"=>21.0, "bar"=>11.0, "stupid"=>13.0}}{"created_at"=>2010-09-28 22:00:00 UTC, "tags"=>{"cool"=>11.0, "dumb"=>16.0, "stupid"=>11.0, "weird"=>15.0, "foo"=>9.0, "bar"=>16.0}}{"created_at"=>2010-09-10 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>9.0, "bar"=>8.0, "cool"=>14.0, "stupid"=>11.0, "foo"=>11.0}}{"created_at"=>2010-09-03 22:00:00 UTC, "tags"=>{"weird"=>14.0, "dumb"=>15.0, "stupid"=>11.0, "foo"=>16.0, "bar"=>20.0, "cool"=>10.0}}{"created_at"=>2010-09-21 22:00:00 UTC, "tags"=>{"weird"=>15.0, "cool"=>14.0, "foo"=>13.0, "stupid"=>6.0, "bar"=>11.0, "dumb"=>9.0}}{"created_at"=>2010-09-23 22:00:00 UTC, "tags"=>{"weird"=>15.0, "stupid"=>15.0, "dumb"=>15.0, "foo"=>16.0, "cool"=>10.0, "bar"=>11.0}}{"created_at"=>2010-09-29 22:00:00 UTC, "tags"=>{"bar"=>9.0, "cool"=>14.0, "weird"=>16.0, "foo"=>8.0, "dumb"=>9.0, "stupid"=>12.0}}{"created_at"=>2010-09-27 22:00:00 UTC, "tags"=>{"cool"=>13.0, "dumb"=>10.0, "stupid"=>12.0, "bar"=>8.0, "foo"=>16.0, "weird"=>13.0}}{"created_at"=>2010-09-04 22:00:00 UTC, "tags"=>{"cool"=>11.0, "bar"=>9.0, "stupid"=>6.0, "weird"=>11.0, "dumb"=>8.0, "foo"=>11.0}}{"created_at"=>2010-09-08 22:00:00 UTC, "tags"=>{"stupid"=>12.0, "dumb"=>11.0, "cool"=>15.0, "foo"=>11.0, "bar"=>9.0, "weird"=>8.0}}{"created_at"=>2010-10-02 22:00:00 UTC, "tags"=>{"bar"=>8.0, "dumb"=>8.0, "cool"=>10.0, "foo"=>10.0, "stupid"=>8.0, "weird"=>6.0}}{"created_at"=>2010-09-24 22:00:00 UTC, "tags"=>{"foo"=>13.0, "bar"=>12.0, "stupid"=>15.0, "weird"=>17.0, "dumb"=>7.0, "cool"=>10.0}}{"created_at"=>2010-09-30 22:00:00 UTC, "tags"=>{"bar"=>10.0, "cool"=>6.0, "stupid"=>14.0, "weird"=>9.0, "dumb"=>12.0, "foo"=>19.0}}{"created_at"=>2010-09-05 22:00:00 UTC, "tags"=>{"dumb"=>12.0, "foo"=>19.0, "weird"=>8.0, "stupid"=>8.0, "bar"=>7.0, "cool"=>10.0}}{"created_at"=>2010-09-17 22:00:00 UTC, "tags"=>{"weird"=>13.0, "bar"=>14.0, "dumb"=>12.0, "foo"=>12.0, "stupid"=>10.0, "cool"=>9.0}}{"created_at"=>2010-09-18 22:00:00 UTC, "tags"=>{"dumb"=>8.0, "cool"=>11.0, "foo"=>6.0, "bar"=>12.0, "weird"=>7.0, "stupid"=>6.0}}{"created_at"=>2010-09-09 22:00:00 UTC, "tags"=>{"weird"=>11.0, "dumb"=>9.0, "foo"=>6.0, "bar"=>11.0, "cool"=>11.0, "stupid"=>6.0}}{"created_at"=>2010-09-13 22:00:00 UTC, "tags"=>{"dumb"=>19.0, "stupid"=>9.0, "weird"=>12.0, "cool"=>11.0, "bar"=>10.0, "foo"=>15.0}}{"created_at"=>2010-09-16 22:00:00 UTC, "tags"=>{"bar"=>6.0, "weird"=>8.0, "dumb"=>9.0, "cool"=>11.0, "stupid"=>17.0, "foo"=>15.0}}{"created_at"=>2010-09-11 22:00:00 UTC, "tags"=>{"foo"=>10.0, "weird"=>9.0, "bar"=>8.0, "cool"=>4.0, "dumb"=>8.0, "stupid"=>9.0}}{"created_at"=>2010-09-26 22:00:00 UTC, "tags"=>{"dumb"=>15.0, "weird"=>6.0, "stupid"=>15.0, "bar"=>10.0, "foo"=>13.0, "cool"=>15.0}}{"created_at"=>2010-10-01 22:00:00 UTC, "tags"=>{"cool"=>7.0, "weird"=>11.0, "stupid"=>11.0, "bar"=>14.0, "foo"=>12.0, "dumb"=>11.0}}{"created_at"=>2010-09-12 22:00:00 UTC, "tags"=>{"bar"=>7.0, "weird"=>12.0, "stupid"=>11.0, "cool"=>10.0, "foo"=>11.0, "dumb"=>9.0}}{"created_at"=>2010-09-14 22:00:00 UTC, "tags"=>{"dumb"=>8.0, "foo"=>15.0, "cool"=>15.0, "stupid"=>15.0, "bar"=>7.0, "weird"=>14.0}}{"created_at"=>2010-09-07 22:00:00 UTC, "tags"=>{"dumb"=>10.0, "cool"=>7.0, "foo"=>14.0, "weird"=>15.0, "bar"=>11.0, "stupid"=>7.0}}{"created_at"=>2010-09-06 22:00:00 UTC, "tags"=>{"dumb"=>7.0, "bar"=>11.0, "cool"=>16.0, "weird"=>14.0, "foo"=>12.0, "stupid"=>6.0}}

"tags" => { "foo" => 11.0, "dumb" => 12.0, "stupid" => 7.0, "bar" => 7.0, "cool" => 14.0, "weird" => 17.0}

Samstag, 23. Oktober 2010

Page 115: Mongodb railscamphh

function(prev) { var mostPopular = 0; for(i in prev.tags) { if(prev.tags[i] > mostPopular) { prev.tag = i; prev.count = prev.tags[i]; mostPopular = prev.tags[i]; } } delete prev.tags}

Samstag, 23. Oktober 2010

Page 116: Mongodb railscamphh

{"created_at"=>2010-09-27 22:00:00 UTC, "tag"=>"stupid", "count"=>18.0}{"created_at"=>2010-09-29 22:00:00 UTC, "tag"=>"stupid", "count"=>20.0}{"created_at"=>2010-09-12 22:00:00 UTC, "tag"=>"cool", "count"=>11.0}{"created_at"=>2010-09-04 22:00:00 UTC, "tag"=>"stupid", "count"=>12.0}{"created_at"=>2010-09-21 22:00:00 UTC, "tag"=>"stupid", "count"=>16.0}{"created_at"=>2010-09-03 22:00:00 UTC, "tag"=>"foo", "count"=>15.0}{"created_at"=>2010-09-26 22:00:00 UTC, "tag"=>"foo", "count"=>17.0}{"created_at"=>2010-09-18 22:00:00 UTC, "tag"=>"foo", "count"=>17.0}{"created_at"=>2010-09-24 22:00:00 UTC, "tag"=>"cool", "count"=>11.0}

Samstag, 23. Oktober 2010

Page 117: Mongodb railscamphh

Map / Reduce

Samstag, 23. Oktober 2010

Page 118: Mongodb railscamphh

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)

Samstag, 23. Oktober 2010

Page 119: Mongodb railscamphh

Indexes

Samstag, 23. Oktober 2010

Page 120: Mongodb railscamphh

db['people'].create_index("tags")

@db['people'].create_index( [["tags", Mongo::ASCENDING]])

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

db['people'].drop_indexes

db['people'].index_information

Samstag, 23. Oktober 2010

Page 121: Mongodb railscamphh

Geospatial stuff

Samstag, 23. Oktober 2010

Page 122: Mongodb railscamphh

@db['people'].create_index( [["latlng", Mongo::GEO2D]])

Samstag, 23. Oktober 2010

Page 123: Mongodb railscamphh

@db['people'].find( "latlng" => {"$near" => [53.593978, 10.107380]})

Samstag, 23. Oktober 2010

Page 124: Mongodb railscamphh

GridFS usage

Samstag, 23. Oktober 2010

Page 125: Mongodb railscamphh

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"))

Samstag, 23. Oktober 2010

Page 126: Mongodb railscamphh

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")

Samstag, 23. Oktober 2010

Page 127: Mongodb railscamphh

Capped collections

Samstag, 23. Oktober 2010

Page 128: Mongodb railscamphh

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

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

Samstag, 23. Oktober 2010

Page 129: Mongodb railscamphh

explain

Samstag, 23. Oktober 2010

Page 130: Mongodb railscamphh

@db['people'].find( "address.city" => /haven/).explain

Samstag, 23. Oktober 2010

Page 131: Mongodb railscamphh

@db['people'].find( "address.city" => /haven/).explain

Samstag, 23. Oktober 2010

Page 132: Mongodb railscamphh

{ "cursor"=>"BasicCursor", "nscanned"=>1000, "nscannedObjects"=>1000, "n"=>39, "millis"=>2, "indexBounds"=>{}, "allPlans"=>[ {"cursor"=>"BasicCursor", "indexBounds"=>{}} ]}

Samstag, 23. Oktober 2010

Page 133: Mongodb railscamphh

{ "cursor"=>"BtreeCursor address.city_1 multi", "nscanned"=>1000, "nscannedObjects"=>39, "n"=>39, "millis"=>1, "indexBounds"=>{ "address.city"=>[["", {}], [/haven/, /haven/]] }, "allPlans"=>[ { "cursor"=>"BtreeCursor address.city_1 multi", "indexBounds"=>{ "address.city"=>[["", {}], [/haven/, /haven/]] } } ]}

Samstag, 23. Oktober 2010

Page 134: Mongodb railscamphh

ODMs

Samstag, 23. Oktober 2010

Page 135: Mongodb railscamphh

mongo_mapper

Samstag, 23. Oktober 2010

Page 136: Mongodb railscamphh

John Nunemaker@jnunemaker

Samstag, 23. Oktober 2010

Page 137: Mongodb railscamphh

is in production

Samstag, 23. Oktober 2010

Page 138: Mongodb railscamphh

documentation?

Samstag, 23. Oktober 2010

Page 139: Mongodb railscamphh

Samstag, 23. Oktober 2010

Page 140: Mongodb railscamphh

how to

Samstag, 23. Oktober 2010

Page 141: Mongodb railscamphh

rails initializer

Samstag, 23. Oktober 2010

Page 142: Mongodb railscamphh

# 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"]

Samstag, 23. Oktober 2010

Page 143: Mongodb railscamphh

a simple example

Samstag, 23. Oktober 2010

Page 144: Mongodb railscamphh

MongoMapper.connection = @connectionMongoMapper.database = "test"

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

Samstag, 23. Oktober 2010

Page 145: Mongodb railscamphh

finders

Samstag, 23. Oktober 2010

Page 146: Mongodb railscamphh

Quote.where(:from => 'Yogi Berra').all

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

Samstag, 23. Oktober 2010

Page 147: Mongodb railscamphh

embedded docs

Samstag, 23. Oktober 2010

Page 148: Mongodb railscamphh

Samstag, 23. Oktober 2010

Page 149: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 150: Mongodb railscamphh

person = Person.firstaddress = Person.first.address

Samstag, 23. Oktober 2010

Page 151: Mongodb railscamphh

scopes

Samstag, 23. Oktober 2010

Page 152: Mongodb railscamphh

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

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

Samstag, 23. Oktober 2010

Page 153: Mongodb railscamphh

new website coming soon

Samstag, 23. Oktober 2010

Page 154: Mongodb railscamphh

mongoid

Samstag, 23. Oktober 2010

Page 155: Mongodb railscamphh

Durran Jordan(of Hashrocket)

Samstag, 23. Oktober 2010

Page 156: Mongodb railscamphh

Two major versions

Samstag, 23. Oktober 2010

Page 157: Mongodb railscamphh

1.x (1.9.x) targeting Rails 2.3.x

Samstag, 23. Oktober 2010

Page 158: Mongodb railscamphh

2.x (2.0beta) targeting Rails 3.0

Samstag, 23. Oktober 2010

Page 159: Mongodb railscamphh

Good documentation

Samstag, 23. Oktober 2010

Page 160: Mongodb railscamphh

Samstag, 23. Oktober 2010

Page 161: Mongodb railscamphh

rails initializer

Samstag, 23. Oktober 2010

Page 162: Mongodb railscamphh

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)

Samstag, 23. Oktober 2010

Page 163: Mongodb railscamphh

a simple example

Samstag, 23. Oktober 2010

Page 164: Mongodb railscamphh

class Quote include Mongoid::Document include Mongoid::Timestamps field :from field :text field :views, :type => Integerend

Samstag, 23. Oktober 2010

Page 165: Mongodb railscamphh

finders

Samstag, 23. Oktober 2010

Page 166: Mongodb railscamphh

Quote.where(:from => 'Yogi Berra').all

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

Samstag, 23. Oktober 2010

Page 167: Mongodb railscamphh

embedded docs

Samstag, 23. Oktober 2010

Page 168: Mongodb railscamphh

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

Samstag, 23. Oktober 2010

Page 169: Mongodb railscamphh

person = Person.firstaddress = Person.first.address

Samstag, 23. Oktober 2010

Page 170: Mongodb railscamphh

scopes

Samstag, 23. Oktober 2010

Page 171: Mongodb railscamphh

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

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

Samstag, 23. Oktober 2010

Page 172: Mongodb railscamphh

More features

Samstag, 23. Oktober 2010

Page 173: Mongodb railscamphh

atomic updates

Samstag, 23. Oktober 2010

Page 174: Mongodb railscamphh

mongoid tries to be clever

Samstag, 23. Oktober 2010

Page 175: Mongodb railscamphh

(using the „dirty“ flags)

Samstag, 23. Oktober 2010

Page 176: Mongodb railscamphh

(it‘s probably better to bypass the ODM sometimes)

Samstag, 23. Oktober 2010

Page 177: Mongodb railscamphh

GridFS

Samstag, 23. Oktober 2010

Page 178: Mongodb railscamphh

external libraries for both

Samstag, 23. Oktober 2010

Page 179: Mongodb railscamphh

mongo_mapper > grip

Samstag, 23. Oktober 2010

Page 180: Mongodb railscamphh

mongoid > mongoid_grid

Samstag, 23. Oktober 2010

Page 181: Mongodb railscamphh

Other noteworthy libraries

Samstag, 23. Oktober 2010

Page 182: Mongodb railscamphh

Other not so noteworthy libraries

Samstag, 23. Oktober 2010

Page 183: Mongodb railscamphh

I ♥

Samstag, 23. Oktober 2010