Hands on with Ruby & MongoDB

58
Hands on with MongoDB Ruby & ^ Austin on Rails October 27, 2009 WYNNNETHERLAND Wednesday, October 28, 2009

description

Slides from talk at Austin on Rails on using MongoDB with Ruby.

Transcript of Hands on with Ruby & MongoDB

Page 1: Hands on with Ruby & MongoDB

Hands on with MongoDBRuby &

^

Austin on Rails ★ October 27, 2009

WYNNNETHERLAND

Wednesday, October 28, 2009

Page 2: Hands on with Ruby & MongoDB

Wednesday, October 28, 2009

Page 3: Hands on with Ruby & MongoDB

No SQL?x

x

Wednesday, October 28, 2009

Page 4: Hands on with Ruby & MongoDB

When does NOSQL make sense?

★Your data is stored and retrieved mainly by primary key, without complex joins.

★You have a non-trivial amount of data, and the thought of managing lots of RDBMS shards and replication failure scenarios gives you the fear.

http://www.metabrew.com/article/anti-rdbms-a-list-of-distributed-key-value-stores/

Wednesday, October 28, 2009

Page 6: Hands on with Ruby & MongoDB

Tokyo Cabinet: Popular with Rubyists, Big in Japan

Wednesday, October 28, 2009

Page 7: Hands on with Ruby & MongoDB

Tokyo Cabinet: Popular with Rubyists, Big in Japan

★Lightning fast★Works best for !at objects★Tokyo Tyrant for network access

http://www.igvita.com/2009/02/13/tokyo-cabinet-beyond-key-value-store/

Wednesday, October 28, 2009

Page 8: Hands on with Ruby & MongoDB

More document-oriented solutions

Wednesday, October 28, 2009

Page 9: Hands on with Ruby & MongoDB

Wednesday, October 28, 2009

Page 10: Hands on with Ruby & MongoDB

CouchDB

Apache CouchDB is a distributed, fault-tolerant and schema-free document-oriented database accessible via a RESTful HTTP/JSON API.

http://couchdb.apache.org/

Wednesday, October 28, 2009

Page 11: Hands on with Ruby & MongoDB

Erlang + Javascript

Wednesday, October 28, 2009

Page 12: Hands on with Ruby & MongoDB

Map + reduce

Wednesday, October 28, 2009

Page 13: Hands on with Ruby & MongoDB

Very cool

Wednesday, October 28, 2009

Page 14: Hands on with Ruby & MongoDB

Plenty o’ Ruby to go around

Wednesday, October 28, 2009

Page 15: Hands on with Ruby & MongoDB

Ruby libraries for CouchDB

CouchRest Basic model RelaxDB CouchPotato CouchFoo ActiveCouch

http://www.slideshare.net/brianthecoder/couchdb

From theAustin on Rails talk

Wednesday, October 28, 2009

Page 16: Hands on with Ruby & MongoDB

Document stores:Throw out everything

you learned about DB design

almost*

* more on this later

Wednesday, October 28, 2009

Page 17: Hands on with Ruby & MongoDB

SQL CouchDB

Prede!ned, explicit schema Dynamic, implicit schema

Uniform tables of data Collection of named documents with varying structure

Normalized. Objects spread across tables. Duplication reduced.

Denormalized. Docs usually self contained. Data often duplicated.

Must know schema to read/write a complete object Must know only document name

Dynamic queries of static schemas Static queries of dynamic schemas

http://damienkatz.net/files/What is CouchDB.pdfhttp://damienkatz.net/files/What is CouchDB.pdf

Wednesday, October 28, 2009

Page 18: Hands on with Ruby & MongoDB

SQL CouchDB

Prede!ned, explicit schema Dynamic, implicit schema

Uniform tables of data Collection of named documents with varying structure

Normalized. Objects spread across tables. Duplication reduced.

Denormalized. Docs usually self contained. Data often duplicated.

Must know schema to read/write a complete object Must know only document name

Dynamic queries of static schemas Static queries of dynamic schemas

http://damienkatz.net/files/What is CouchDB.pdfhttp://damienkatz.net/files/What is CouchDB.pdf

The devil's in the details

Wednesday, October 28, 2009

Page 19: Hands on with Ruby & MongoDB

Wednesday, October 28, 2009

Page 20: Hands on with Ruby & MongoDB

because this is BIG

Wednesday, October 28, 2009

Page 21: Hands on with Ruby & MongoDB

Wednesday, October 28, 2009

Page 22: Hands on with Ruby & MongoDB

Runs like Hayes.Hits like Mays.

Wednesday, October 28, 2009

Page 23: Hands on with Ruby & MongoDB

MongoDB x

Wednesday, October 28, 2009

Page 24: Hands on with Ruby & MongoDB

Let's just skip to here already!

x

Wednesday, October 28, 2009

Page 25: Hands on with Ruby & MongoDB

Introducing

★Built For Speed★Dynamic Queries and Indexes★Replication and Failover★Sharding★Map / Reduce

can be very fast

Wednesday, October 28, 2009

Page 26: Hands on with Ruby & MongoDB

MongoDB is great for

★Websites

★Caching

★High volume, low value

★High scalability

★Storage of program objects and JSON

stash the hash

Wednesday, October 28, 2009

Page 27: Hands on with Ruby & MongoDB

Not as great for

★Highly transactional

★Ad-hoc business intelligence

★Problems requiring SQL

Wednesday, October 28, 2009

Page 28: Hands on with Ruby & MongoDB

Installation

★mkdir -p /data/db

★download pre-built for OSX and unzip to /usr/local/

★cp -R /usr/local/pathtomongo/bin /usr/local/bin

★sudo gem install mongo

★sudo gem install mongo_ext

★sudo gem install mongo_mapper

Native C extensions ( go turbo! )

Ruby driver for MongoDB

Wednesday, October 28, 2009

Page 29: Hands on with Ruby & MongoDB

Contents of mongo/bin

★mongod - The MongoDB server

★mongo - the JavaScript interactive shell

★mongoexport - export data as JSON or csv

★mongoimport - As advertised

★mongodump - Like mysqldump

★mongorestore - Restore from mongodump "les

★mongos - Auto-sharding module (getting better with every build)

what's in the box?

why? What did you think it was?

Wednesday, October 28, 2009

Page 30: Hands on with Ruby & MongoDB

Some new terms

Wednesday, October 28, 2009

Page 31: Hands on with Ruby & MongoDB

When I saydatabase

Wednesday, October 28, 2009

Page 32: Hands on with Ruby & MongoDB

When I saydatabase

thinkdatabase

Wednesday, October 28, 2009

Page 33: Hands on with Ruby & MongoDB

Well that one isn't new...

When I saydatabase

thinkdatabase

Wednesday, October 28, 2009

Page 34: Hands on with Ruby & MongoDB

Databases in MongoDB

★Made up of multiple collections

★Are created on-the-!y when "rst referenced

Wednesday, October 28, 2009

Page 35: Hands on with Ruby & MongoDB

When I saycollection

Wednesday, October 28, 2009

Page 36: Hands on with Ruby & MongoDB

When I saycollection

thinktable

Wednesday, October 28, 2009

Page 37: Hands on with Ruby & MongoDB

Collections in MongoDB

★Schema-less

★For grouping documents into smaller query sets (speed)

★ Indexable by one or more key

★Are created on-the-!y when "rst referenced

★Capped collections: Fixed size, older records dropped after limit reached

but typed!

Wednesday, October 28, 2009

Page 38: Hands on with Ruby & MongoDB

When I saydocument

Wednesday, October 28, 2009

Page 39: Hands on with Ruby & MongoDB

When I saydocument

thinkrecord or row

Wednesday, October 28, 2009

Page 40: Hands on with Ruby & MongoDB

Document

★Stored in a collection

★Can have _id key that works like primary keys in MySQL

★Supports relationships: subdocument or db reference

Wednesday, October 28, 2009

Page 41: Hands on with Ruby & MongoDB

Document Storage (BSON)

{ author: 'joe', created: Date('03-28-2009'), title: 'Yet another blog post', text: 'Here is the text...', tags: [ 'example', 'joe' ], comments: [ { author: 'jim', comment: 'I disagree' }, { author: 'nancy', comment: 'Good post' } ]}

http://www.mongodb.org/display/DOCS/BSON

Wednesday, October 28, 2009

Page 42: Hands on with Ruby & MongoDB

Document Storage (BSON)

{ author: 'joe', created: Date('03-28-2009'), title: 'Yet another blog post', text: 'Here is the text...', tags: [ 'example', 'joe' ], comments: [ { author: 'jim', comment: 'I disagree' }, { author: 'nancy', comment: 'Good post' } ]}

http://www.mongodb.org/display/DOCS/BSON

Sure wish JSON did this...

Wednesday, October 28, 2009

Page 43: Hands on with Ruby & MongoDB

Document Storage (BSON)

{ author: 'joe', created: Date('03-28-2009'), title: 'Yet another blog post', text: 'Here is the text...', tags: [ 'example', 'joe' ], comments: [ { author: 'jim', comment: 'I disagree' }, { author: 'nancy', comment: 'Good post' } ]}

http://www.mongodb.org/display/DOCS/BSON

Sure wish JSON did this...

B is for Binary

Wednesday, October 28, 2009

Page 44: Hands on with Ruby & MongoDB

That looks like JSON

Wednesday, October 28, 2009

Page 45: Hands on with Ruby & MongoDB

So does this

jason.to_json

Wednesday, October 28, 2009

Page 46: Hands on with Ruby & MongoDB

Where's the Ruby?

Wednesday, October 28, 2009

Page 47: Hands on with Ruby & MongoDB

Querying

db.collection.find({'first_name': 'John'}) # finds all Johns

db.collection.find({'first_name': /^wynn/i}) # regex

db.collection.find_first({'_id':1}) # finds first with _id of 1

db.collection.find({'age': {'$gte': 21}}) # finds possible drinkers

db.collection.find({'author.first_name':'John'}) # subdocument

db.collection.find({$where:'this.age >= 6 && this.age <= 18'})

Wednesday, October 28, 2009

Page 48: Hands on with Ruby & MongoDB

Querying

$in, $nin, $all, $ne, $gt, $gte, $lt, $lte, $size, $where

:fields (like :select in active record)

:limit, :offset for pagination

:sort ascending or descending [['foo', 1], ['bar', -1]]

count and group (uses map/reduce)

db.collection.mapReduce(mapfunction,reducefunction[,options]);

More

Wednesday, October 28, 2009

Page 50: Hands on with Ruby & MongoDB

MongoMapper

★Mongo is not MySQL

★DSL for modeling domain should also teach you Mongo

★ It sounded fun

★Just released version 0.5.6

from @jnunemaker

Wednesday, October 28, 2009

Page 51: Hands on with Ruby & MongoDB

MongoMapper

★Mongo is not MySQL

★DSL for modeling domain should also teach you Mongo

★ It sounded fun

★Just released version 0.5.6

from @jnunemaker

I voted for "Nunemapper"

Wednesday, October 28, 2009

Page 52: Hands on with Ruby & MongoDB

Features

★Typecasting

★Callbacks (ActiveSupport Callbacks)

★Validations

★Connection and database can differ per document

★Create and Update with single or multiple

★Delete and Destroy and _all counterparts

★Find: id, ids, :all, :"rst, :last

★Associations

Be careful. Ordering can be tricky.

Wednesday, October 28, 2009

Page 53: Hands on with Ruby & MongoDB

Example

class User include MongoMapper::Document key :name, String, :required => true, :length => 5..100 key :email, String, :required => true, :index => true key :age, Integer, :numeric => true key :active, Boolean, :default => true one :address many :articlesend

class Address include MongoMapper::Document key :street, String key :city, String key :state, String, :length => 2 key :zip, Integer, :numeric => true, :length => 5end

Included as module, not subclassed (this may change soon).

Wednesday, October 28, 2009

Page 54: Hands on with Ruby & MongoDB

MongoDB fun

★Capped collections (think memcache, actually used for replication)

★Upserts db.collection.update({'_id':1}, {'$inc':{'views':1}})

★Multikeys (think tagging and full text search)

★GridFS and auto-sharding

Wednesday, October 28, 2009

Page 55: Hands on with Ruby & MongoDB

So, is it ready for Prime Time?

★Disqus

★SourceForge

★TweetCongress, GovTwit -- Floxee.com

★TweetSaver.com

★Mozilla Ubiquity Herd

Ask these folks!

Wednesday, October 28, 2009

Page 56: Hands on with Ruby & MongoDB

Lessons learned in production

★The laws of computing are still in effect

★ Indexes are important no matter what the salesman told ya about performance

★Data modeling. Deep or Wide?

★MongoDB and MongoMapper are in active development

the fine print

The answer is yes!

Very responsive yet very volatile changes!

Wednesday, October 28, 2009

Page 57: Hands on with Ruby & MongoDB

How can you help?

★We need an awesome admin GUI

★Port some plugins (might get easier with ActiveModel support coming soon)

★Build something cool

Wednesday, October 28, 2009

Page 58: Hands on with Ruby & MongoDB

Resources

http://mongodb.orghttp://www.10gen.com http://groups.google.com/group/mongomapperhttp://groups.google.com/group/mongodb-userhttp://squeejee.com

http://wynnnetherland.com

and thanks for having me!

Questions? I'm @pengwynn on Twitter

the very new blog

the very cool company behind MongoDB

Wednesday, October 28, 2009