Mongodb

18
MongoDB Getting started, Couch, and MongoMapper

description

10 Minute presentation to sdruby on mongodb and mongomapper

Transcript of Mongodb

Page 1: Mongodb

MongoDBGetting started, Couch, and MongoMapper

Page 2: Mongodb

Who am I?

• Scott Motte / 25 / Perris, CA

• mid-level rubyist that prefers merb

• spitfiresky.com

• twitter.com/spitfiresky

• github.com/scottmotte

[email protected]

Page 3: Mongodb

Leopard Install (0.9.7)

• mkdir -p /data/db

• wget http://downloads.mongodb.org/osx/mongodb-osx-i386-0.9.7.tgz

• sudo tar xvzf mongodb-osx-i386-0.9.7.tgz -C /usr/local

• sudo cp -R /usr/local/mongodb-osx-i386-0.9.7/bin/ /usr/local/bin

Page 4: Mongodb

Linux Install (0.9.7)

• mkdir -p /data/db

• wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-0.9.6.tgz

• sudo tar -zxvf mongodb-linux-x86_64-0.9.7.tgz -C /usr/local

• sudo chmod 755 -R /usr/local/mongodb-linux-x86_64-0.9.7

• sudo cp -R /usr/local/mongodb-linux-x86_64-0.9.7/bin/* /usr/local/bin

Page 5: Mongodb

Running it

• sudo mongod run &

• mongo (mysql-like command line)

• use bookstore_development

• db.books.save({ title: "Ender's Game", description: 'zero gravity and mind games' })

• db.books.findOne()

Page 6: Mongodb

Mongo or CouchMongodb (C++) Couchdb (Erlang)

drivers REST

bson, document, schema-free json, document, schema-free

Dynamic queries, indexing map/reduce

gridfs (needs an apache/nginx module) attachments

RAM http cache

Good at the web, faster development time

Good at the web, slower development time

Update in place (good for high update rates)

MVCC (fault tolerant, but requires compacting)

master-master replication

50s kid indy kid

*http://www.mongodb.org/display/DOCS/Comparing+Mongo+DB+and+Couch+DB

Page 7: Mongodb

Mongodb ormsRuby Python

mongo-ruby-driversudo gem install mongodb-mongo

sudo gem install mongodb-mongo_ext (c extension)

mongo-python-drivereasy_install pymongo (c extension auto-

installed)

active-record-adapter http://github.com/-mongodb/activerecord-mongo-adapter

autumn http://autumn-orm.org/

mongorecord http://github.com/mongodb/mongo-activerecord-ruby

mongo-mapperhttp://github.com/jeffjenkins/mongo-mapper

mongomapperhttp://github.com/jnunemaker/mongomapper

Page 8: Mongodb

MongoMapper

sudo gem install mongomapper

config.gem 'jnunemaker-mongomapper' #rails

dependency 'jnunemaker-mongomapper' #merb

Page 9: Mongodb

Model

class Book include MongoMapper::Document key :title, String key :description, String end

Page 10: Mongodb

Controller

class Books < Application def index @books = Book.all display @books end

def show(id) @book = Book.find(id) raise NotFound unless @book display @book end ...

Page 11: Mongodb

Validationsclass Book include MongoMapper::Document key :title, String key :description, String

validates_presence_of :title #validates_numericality_of #validates_length_of #validates_format_of #moreend

*http://github.com/jnunemaker/validatable

Page 12: Mongodb

Callbacks

*http://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html

class Book .. key :description, String

before_save :append_signature def append_signature self.description << " ~Corner Bookstore" end #after_save #before_validationend

Page 13: Mongodb

Relationshipsclass Book include MongoMapper::Document key :title, String key :description, String has_many :reviewsend

class Reviewinclude MongoMapper::Documentkey :author, Stringkey :review, Stringbelongs_to :book

end

Page 14: Mongodb

Relationships cont.Finding@book = Book.first@reviews = @book.reviews

[email protected] do |review| review.authorend@reviews[0] # return first review

Be [email protected] #slowTweet.count(:user_id => @user.id) #fast

Page 15: Mongodb

Embedded Documentsclass Review include MongoMapper::EmbeddedDocument

key :uuid, String, :default => XGen::Mongo::Driver::ObjectID.new key :author, String key :review, String key :created_at, Time, :default => Time.now.utc before_validation do self.uuid = XGen::Mongo::Driver::ObjectID.new endend

*http://groups.google.com/group/mongomapper/browse_thread/thread/178b8c5105ebedd8

Page 16: Mongodb

Embedded Docs cont.class Reviews < Application .. def create(review) @flight = Flight.find(params['flight_id']) @review = Review.new(review) if @review.valid? && @flight.reviews << @review && @flight.save redirect '/wherever’' :message => {:notice => "Review made"} else message[:error] = "Review fail" render :new end endend# in router.rbresources :flights, :identify => :id do resources :reviews, :identify => :uuidend # /flights/:flight_id/comments/new

Page 17: Mongodb

Additional info

• created_at and updated_at are included automatically by MongoMapper

• _id cannot currently be set with MongoMapper like it can in Couchrest

• cannot currently do @doc[‘custom_field’] like in couchrest.

• indexing: @doc.ensure_index :login

Page 18: Mongodb

ConclusionMongodb is a great trade off of speed, features, and schema-less freedom, and it now has its developer friendly orm - mongomapper.

Strongly consider using it in a web app you otherwise by default would use mysql.

Then put together your models and use script/server or bin/merb -i to test your performance improvements.

~ Scott Motte