213
on ruby mongo berlin 2010 jan krutisch <[email protected] > http://jan.krutisch.de / and rails Donnerstag, 7. Oktober 2010

MongoDB on Rails (and Ruby)

Embed Size (px)

DESCRIPTION

35 minutes of compressed knowledge on how to use mongodb from ruby.

Citation preview

Page 1: MongoDB on Rails (and Ruby)

on ruby

mongo berlin 2010

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

and rails

Donnerstag, 7. Oktober 2010

Page 2: MongoDB on Rails (and Ruby)

I

Donnerstag, 7. Oktober 2010

Page 3: MongoDB on Rails (and Ruby)

I

Web Developer

Donnerstag, 7. Oktober 2010

Page 4: MongoDB on Rails (and Ruby)

I

Web Developer

Donnerstag, 7. Oktober 2010

Page 5: MongoDB on Rails (and Ruby)

I

Web Developer

github.com/halfbyte

Donnerstag, 7. Oktober 2010

Page 6: MongoDB on Rails (and Ruby)

I

Web Developer

jan.krutisch.de

github.com/halfbyte

Donnerstag, 7. Oktober 2010

Page 7: MongoDB on Rails (and Ruby)

I

Web Developer

Tinkerer

jan.krutisch.de

github.com/halfbyte

Donnerstag, 7. Oktober 2010

Page 8: MongoDB on Rails (and Ruby)

I

Web Developer

Tinkerer

twitter.com/halfbyte

jan.krutisch.de

github.com/halfbyte

Donnerstag, 7. Oktober 2010

Page 9: MongoDB on Rails (and Ruby)

ruby

Donnerstag, 7. Oktober 2010

Page 10: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 11: MongoDB on Rails (and Ruby)

Basic driver usage

Donnerstag, 7. Oktober 2010

Page 12: MongoDB on Rails (and Ruby)

gem install mongo bson_ext

Donnerstag, 7. Oktober 2010

Page 13: MongoDB on Rails (and Ruby)

init

Donnerstag, 7. Oktober 2010

Page 14: MongoDB on Rails (and Ruby)

getting connections

Donnerstag, 7. Oktober 2010

Page 15: MongoDB on Rails (and Ruby)

@connection = Mongo::Connection.new

Donnerstag, 7. Oktober 2010

Page 16: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 17: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 18: MongoDB on Rails (and Ruby)

Choose a database.

Donnerstag, 7. Oktober 2010

Page 19: MongoDB on Rails (and Ruby)

@connection.database_names#=> ["admin", "local"]

Donnerstag, 7. Oktober 2010

Page 20: MongoDB on Rails (and Ruby)

@db = @connection['test']

Donnerstag, 7. Oktober 2010

Page 21: MongoDB on Rails (and Ruby)

@db = @connection.db('test')

Donnerstag, 7. Oktober 2010

Page 22: MongoDB on Rails (and Ruby)

collections

Donnerstag, 7. Oktober 2010

Page 23: MongoDB on Rails (and Ruby)

@collection = @db['books']

Donnerstag, 7. Oktober 2010

Page 24: MongoDB on Rails (and Ruby)

@collection = @db.collection('books')

Donnerstag, 7. Oktober 2010

Page 25: MongoDB on Rails (and Ruby)

subcollections

Donnerstag, 7. Oktober 2010

Page 26: MongoDB on Rails (and Ruby)

@collection = @db['books.reviews']

Donnerstag, 7. Oktober 2010

Page 27: MongoDB on Rails (and Ruby)

@collection = @db['books']['reviews']

Donnerstag, 7. Oktober 2010

Page 28: MongoDB on Rails (and Ruby)

C R U D

Donnerstag, 7. Oktober 2010

Page 29: MongoDB on Rails (and Ruby)

Create Read Update Delete

Donnerstag, 7. Oktober 2010

Page 30: MongoDB on Rails (and Ruby)

Create Read Update Delete

Donnerstag, 7. Oktober 2010

Page 31: MongoDB on Rails (and Ruby)

insert

Donnerstag, 7. Oktober 2010

Page 32: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 33: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 34: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 35: MongoDB on Rails (and Ruby)

safe (using getlasterror)

Donnerstag, 7. Oktober 2010

Page 36: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 37: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 38: MongoDB on Rails (and Ruby)

how about getting back an id?

Donnerstag, 7. Oktober 2010

Page 39: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 40: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 41: MongoDB on Rails (and Ruby)

Create Read Update Delete

Donnerstag, 7. Oktober 2010

Page 42: MongoDB on Rails (and Ruby)

Create Read Update Delete

Donnerstag, 7. Oktober 2010

Page 43: MongoDB on Rails (and Ruby)

Create Read Update Delete

Donnerstag, 7. Oktober 2010

Page 44: MongoDB on Rails (and Ruby)

upsert

Donnerstag, 7. Oktober 2010

Page 45: MongoDB on Rails (and Ruby)

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

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

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

Donnerstag, 7. Oktober 2010

Page 46: MongoDB on Rails (and Ruby)

Create Read Update Delete

Donnerstag, 7. Oktober 2010

Page 47: MongoDB on Rails (and Ruby)

Create Read Update Delete

Donnerstag, 7. Oktober 2010

Page 48: MongoDB on Rails (and Ruby)

@db['quotes'].update( {:_id => doc['_id']}, { :from => "Yogi Berra", :text => "You can observe a lot just by watching.", :tags => ['baseball', 'wit'] })

Donnerstag, 7. Oktober 2010

Page 49: MongoDB on Rails (and Ruby)

atomic updates

Donnerstag, 7. Oktober 2010

Page 50: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 51: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 52: MongoDB on Rails (and Ruby)

$inc$set$unset$push$pushAll

$addToSet$pop$pull$pullAll$

Donnerstag, 7. Oktober 2010

Page 53: MongoDB on Rails (and Ruby)

@db['people'].update( {"tags" => "cool"}, "$addToSet" => {"tags" => 'froody'}, :multi => true)

Donnerstag, 7. Oktober 2010

Page 54: MongoDB on Rails (and Ruby)

@db['people'].update( {"tags" => "cool"}, "$addToSet" => {"tags" => 'froody'}, :multi => true)

Donnerstag, 7. Oktober 2010

Page 55: MongoDB on Rails (and Ruby)

@db['people'].update( {"tags" => "cool"}, { "$addToSet" => { "tags" => { "$each" => ['froody', 'hoopy'] } } }, :safe => true, :multi => true)

Donnerstag, 7. Oktober 2010

Page 56: MongoDB on Rails (and Ruby)

@db['people'].update( {"tags" => "cool"}, { "$addToSet" => { "tags" => { "$each" => ['froody', 'hoopy'] } } }, :safe => true, :multi => true)

Donnerstag, 7. Oktober 2010

Page 57: MongoDB on Rails (and Ruby)

@db['people'].update( {"tags" => "cool"}, {"$set" => {"tags.$" => "fresh"}}, :safe => true, :multi => true)

Donnerstag, 7. Oktober 2010

Page 58: MongoDB on Rails (and Ruby)

@db['people'].update( {"tags" => "cool"}, {"$set" => {"tags.$" => "fresh"}}, :safe => true, :multi => true)

Donnerstag, 7. Oktober 2010

Page 59: MongoDB on Rails (and Ruby)

Create Read Update Delete

Donnerstag, 7. Oktober 2010

Page 60: MongoDB on Rails (and Ruby)

Create Read Update Delete

Donnerstag, 7. Oktober 2010

Page 61: MongoDB on Rails (and Ruby)

@db['people'].remove

Donnerstag, 7. Oktober 2010

Page 62: MongoDB on Rails (and Ruby)

@db['numbers'].remove( {"$lt" => 30}, :safe => true)

Donnerstag, 7. Oktober 2010

Page 63: MongoDB on Rails (and Ruby)

Create Read Update Delete

Donnerstag, 7. Oktober 2010

Page 64: MongoDB on Rails (and Ruby)

Create Read Update Delete

Donnerstag, 7. Oktober 2010

Page 65: MongoDB on Rails (and Ruby)

all

Donnerstag, 7. Oktober 2010

Page 66: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 67: MongoDB on Rails (and Ruby)

one

Donnerstag, 7. Oktober 2010

Page 68: MongoDB on Rails (and Ruby)

row = @db['quotes'].find_one

Donnerstag, 7. Oktober 2010

Page 69: MongoDB on Rails (and Ruby)

exact query

Donnerstag, 7. Oktober 2010

Page 70: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 71: MongoDB on Rails (and Ruby)

more queries

Donnerstag, 7. Oktober 2010

Page 72: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 73: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 74: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 75: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 76: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 77: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 78: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 79: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 80: MongoDB on Rails (and Ruby)

Let‘s go deep.

Donnerstag, 7. Oktober 2010

Page 81: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 82: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 83: MongoDB on Rails (and Ruby)

/stand back/

Donnerstag, 7. Oktober 2010

Page 84: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 85: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 86: MongoDB on Rails (and Ruby)

I‘m in ur database, executin ur javascript

Donnerstag, 7. Oktober 2010

Page 87: MongoDB on Rails (and Ruby)

@db['numbers'].find("$where" => "this.i < 2")

Donnerstag, 7. Oktober 2010

Page 88: MongoDB on Rails (and Ruby)

boolsh

Donnerstag, 7. Oktober 2010

Page 89: MongoDB on Rails (and Ruby)

not

Donnerstag, 7. Oktober 2010

Page 90: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 91: MongoDB on Rails (and Ruby)

and

Donnerstag, 7. Oktober 2010

Page 92: MongoDB on Rails (and Ruby)

@db['numbers'].find( "i" => { "$lt" => 52, "$gt" => 48 })

Donnerstag, 7. Oktober 2010

Page 93: MongoDB on Rails (and Ruby)

or

Donnerstag, 7. Oktober 2010

Page 94: MongoDB on Rails (and Ruby)

@db['numbers'].find( "$or" => [ { "i" => { "$lt" => 2 } }, { "i" => { "$gt" => 97 } } ])

Donnerstag, 7. Oktober 2010

Page 95: MongoDB on Rails (and Ruby)

Sorting

Donnerstag, 7. Oktober 2010

Page 96: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 97: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 98: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 99: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 100: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 101: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 102: MongoDB on Rails (and Ruby)

@db['people'].find().sort( "address.street", Mongo::ASCENDING)

Donnerstag, 7. Oktober 2010

Page 103: MongoDB on Rails (and Ruby)

@db['people'].find().sort( "address.street", Mongo::ASCENDING)

Donnerstag, 7. Oktober 2010

Page 104: MongoDB on Rails (and Ruby)

Pagination

Donnerstag, 7. Oktober 2010

Page 105: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 106: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 107: MongoDB on Rails (and Ruby)

Aggregation

Donnerstag, 7. Oktober 2010

Page 108: MongoDB on Rails (and Ruby)

Countin‘ Bizness

Donnerstag, 7. Oktober 2010

Page 109: MongoDB on Rails (and Ruby)

@db['numbers'].find.count

Donnerstag, 7. Oktober 2010

Page 110: MongoDB on Rails (and Ruby)

Distinct

Donnerstag, 7. Oktober 2010

Page 111: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 112: MongoDB on Rails (and Ruby)

Group

Donnerstag, 7. Oktober 2010

Page 113: MongoDB on Rails (and Ruby)

Poor mans map/reduce

Donnerstag, 7. Oktober 2010

Page 114: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 115: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 116: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 117: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 118: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 119: MongoDB on Rails (and Ruby)

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 } }}

Donnerstag, 7. Oktober 2010

Page 120: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 121: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 122: MongoDB on Rails (and Ruby)

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}

Donnerstag, 7. Oktober 2010

Page 123: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 124: MongoDB on Rails (and Ruby)

Map / Reduce

Donnerstag, 7. Oktober 2010

Page 125: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 126: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 127: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 128: MongoDB on Rails (and Ruby)

Indexes

Donnerstag, 7. Oktober 2010

Page 129: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 130: MongoDB on Rails (and Ruby)

Geospatial stuff

Donnerstag, 7. Oktober 2010

Page 131: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 132: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 133: MongoDB on Rails (and Ruby)

GridFS usage

Donnerstag, 7. Oktober 2010

Page 134: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 135: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 136: MongoDB on Rails (and Ruby)

Capped collections

Donnerstag, 7. Oktober 2010

Page 137: MongoDB on Rails (and Ruby)

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

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

Donnerstag, 7. Oktober 2010

Page 138: MongoDB on Rails (and Ruby)

explain

Donnerstag, 7. Oktober 2010

Page 139: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 140: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 141: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 142: MongoDB on Rails (and Ruby)

{ "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/]] } } ]}

Donnerstag, 7. Oktober 2010

Page 143: MongoDB on Rails (and Ruby)

misc commands

Donnerstag, 7. Oktober 2010

Page 144: MongoDB on Rails (and Ruby)

@db.stats

{ "collections"=>4, "objects"=>1011, "avgObjSize"=>244.6330365974283, "dataSize"=>247324, "storageSize"=>345600, "numExtents"=>6, "indexes"=>3, "indexSize"=>114688, "fileSize"=>201326592, "ok"=>1.0}

Donnerstag, 7. Oktober 2010

Page 145: MongoDB on Rails (and Ruby)

@db.add_stored_function( "tag_size", "function(obj) {return obj.tags.length}")

@db['people'].find( "$where" => 'tag_size(this) === 2')

Donnerstag, 7. Oktober 2010

Page 146: MongoDB on Rails (and Ruby)

@db.add_stored_function( "tag_size", "function(obj) {return obj.tags.length}")

@db['people'].find( "$where" => 'tag_size(this) === 2')

Donnerstag, 7. Oktober 2010

Page 147: MongoDB on Rails (and Ruby)

Libraries

Donnerstag, 7. Oktober 2010

Page 148: MongoDB on Rails (and Ruby)

mongo_mapper

Donnerstag, 7. Oktober 2010

Page 149: MongoDB on Rails (and Ruby)

John Nunemaker@jnunemaker

Donnerstag, 7. Oktober 2010

Page 150: MongoDB on Rails (and Ruby)

is in production

Donnerstag, 7. Oktober 2010

Page 151: MongoDB on Rails (and Ruby)

documentation?

Donnerstag, 7. Oktober 2010

Page 152: MongoDB on Rails (and Ruby)

Donnerstag, 7. Oktober 2010

Page 153: MongoDB on Rails (and Ruby)

how to

Donnerstag, 7. Oktober 2010

Page 154: MongoDB on Rails (and Ruby)

rails initializer

Donnerstag, 7. Oktober 2010

Page 155: MongoDB on Rails (and Ruby)

# config/initializers/mongo_mapper.rbmongo_config = YAML.load_file( File.join(Rails.root, 'config','mongomapper.yml'))MongoMapper.setup(mongo_config, Rails.env)

Donnerstag, 7. Oktober 2010

Page 156: MongoDB on Rails (and Ruby)

a simple example

Donnerstag, 7. Oktober 2010

Page 157: MongoDB on Rails (and Ruby)

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

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

Donnerstag, 7. Oktober 2010

Page 158: MongoDB on Rails (and Ruby)

finders

Donnerstag, 7. Oktober 2010

Page 159: MongoDB on Rails (and Ruby)

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

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

Donnerstag, 7. Oktober 2010

Page 160: MongoDB on Rails (and Ruby)

embedded docs

Donnerstag, 7. Oktober 2010

Page 161: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 162: MongoDB on Rails (and Ruby)

person = Person.firstaddress = Person.first.address

Donnerstag, 7. Oktober 2010

Page 163: MongoDB on Rails (and Ruby)

scopes

Donnerstag, 7. Oktober 2010

Page 164: MongoDB on Rails (and Ruby)

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

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

Donnerstag, 7. Oktober 2010

Page 165: MongoDB on Rails (and Ruby)

new website coming soon

Donnerstag, 7. Oktober 2010

Page 166: MongoDB on Rails (and Ruby)

new website coming soonUnfortunately I‘ve already said that

@ FrOSCON in August

Donnerstag, 7. Oktober 2010

Page 167: MongoDB on Rails (and Ruby)

new website coming soonUnfortunately I‘ve already said that

@ FrOSCON in August

Donnerstag, 7. Oktober 2010

Page 168: MongoDB on Rails (and Ruby)

mongoid

Donnerstag, 7. Oktober 2010

Page 169: MongoDB on Rails (and Ruby)

Durran Jordan(of Hashrocket)

Donnerstag, 7. Oktober 2010

Page 170: MongoDB on Rails (and Ruby)

Two major versions

Donnerstag, 7. Oktober 2010

Page 171: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 172: MongoDB on Rails (and Ruby)

2.x (2.0beta) targeting Rails 3.0

Donnerstag, 7. Oktober 2010

Page 173: MongoDB on Rails (and Ruby)

Good documentation

Donnerstag, 7. Oktober 2010

Page 174: MongoDB on Rails (and Ruby)

Donnerstag, 7. Oktober 2010

Page 175: MongoDB on Rails (and Ruby)

rails initializer

Donnerstag, 7. Oktober 2010

Page 176: MongoDB on Rails (and Ruby)

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)

Donnerstag, 7. Oktober 2010

Page 177: MongoDB on Rails (and Ruby)

a simple example

Donnerstag, 7. Oktober 2010

Page 178: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 179: MongoDB on Rails (and Ruby)

finders

Donnerstag, 7. Oktober 2010

Page 180: MongoDB on Rails (and Ruby)

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

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

Donnerstag, 7. Oktober 2010

Page 181: MongoDB on Rails (and Ruby)

embedded docs

Donnerstag, 7. Oktober 2010

Page 182: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 183: MongoDB on Rails (and Ruby)

person = Person.firstaddress = Person.first.address

Donnerstag, 7. Oktober 2010

Page 184: MongoDB on Rails (and Ruby)

scopes

Donnerstag, 7. Oktober 2010

Page 185: MongoDB on Rails (and Ruby)

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

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

Donnerstag, 7. Oktober 2010

Page 186: MongoDB on Rails (and Ruby)

More features

Donnerstag, 7. Oktober 2010

Page 187: MongoDB on Rails (and Ruby)

atomic updates

Donnerstag, 7. Oktober 2010

Page 188: MongoDB on Rails (and Ruby)

mongoid tries to be clever

Donnerstag, 7. Oktober 2010

Page 189: MongoDB on Rails (and Ruby)

(using the „dirty“ flags)

Donnerstag, 7. Oktober 2010

Page 190: MongoDB on Rails (and Ruby)

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

Donnerstag, 7. Oktober 2010

Page 191: MongoDB on Rails (and Ruby)

GridFS

Donnerstag, 7. Oktober 2010

Page 192: MongoDB on Rails (and Ruby)

external libraries for both

Donnerstag, 7. Oktober 2010

Page 193: MongoDB on Rails (and Ruby)

mongo_mapper > grip

Donnerstag, 7. Oktober 2010

Page 194: MongoDB on Rails (and Ruby)

mongoid > mongoid_grid

Donnerstag, 7. Oktober 2010

Page 195: MongoDB on Rails (and Ruby)

validations

Donnerstag, 7. Oktober 2010

Page 196: MongoDB on Rails (and Ruby)

both through ActiveModel or validatable (Rails <3)

Donnerstag, 7. Oktober 2010

Page 197: MongoDB on Rails (and Ruby)

Other noteworthy libraries

Donnerstag, 7. Oktober 2010

Page 198: MongoDB on Rails (and Ruby)

The „ORM“ variant

Donnerstag, 7. Oktober 2010

Page 199: MongoDB on Rails (and Ruby)

mongodoc

http://github.com/leshill/mongodoc

Donnerstag, 7. Oktober 2010

Page 200: MongoDB on Rails (and Ruby)

mongo-record

http://github.com/mongodb/mongo-record

Donnerstag, 7. Oktober 2010

Page 201: MongoDB on Rails (and Ruby)

mongomodel

http://github.com/spohlenz/mongomodel

Donnerstag, 7. Oktober 2010

Page 202: MongoDB on Rails (and Ruby)

mongomatic

http://github.com/benmyles/mongomatic

Donnerstag, 7. Oktober 2010

Page 203: MongoDB on Rails (and Ruby)

Queues

Donnerstag, 7. Oktober 2010

Page 204: MongoDB on Rails (and Ruby)

mongo_queue

http://github.com/Skiz/mongo_queue

Donnerstag, 7. Oktober 2010

Page 205: MongoDB on Rails (and Ruby)

mongo_queue

http://github.com/Skiz/mongo_queue

Last commit on github.com in March...

Donnerstag, 7. Oktober 2010

Page 206: MongoDB on Rails (and Ruby)

resque-mongo

http://github.com/ctrochalakis/resque-mongo

Donnerstag, 7. Oktober 2010

Page 207: MongoDB on Rails (and Ruby)

resque-mongo

http://github.com/ctrochalakis/resque-mongo

Last commit on github.com in January...

Donnerstag, 7. Oktober 2010

Page 208: MongoDB on Rails (and Ruby)

Misc

Donnerstag, 7. Oktober 2010

Page 209: MongoDB on Rails (and Ruby)

em-mongo

http://github.com/bcg/em-mongo

Donnerstag, 7. Oktober 2010

Page 210: MongoDB on Rails (and Ruby)

dm-mongo-adapter

http://github.com/solnic/dm-mongo-adapter

Donnerstag, 7. Oktober 2010

Page 211: MongoDB on Rails (and Ruby)

questions? injuries?

Donnerstag, 7. Oktober 2010

Page 212: MongoDB on Rails (and Ruby)

I ♥

Donnerstag, 7. Oktober 2010