Top Banner
MongoDB Getting started, Couch, and MongoMapper
18

MongoDB

Nov 17, 2014

Download

Documents

Scott Motte introduces us to the document-oriented database MongoDB and shows how to use MongoDB as an alternative to MySQL and ActiveRecord using the MongoMapper gem.

This talk was part of the DjangoSD/SD Ruby mashup meeting.

Watch a video at http://www.bestechvideos.com/2009/12/29/sd-ruby-episode-72-mongodb
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

MongoDBGetting started, Couch, and MongoMapper

Who am I? Scott Motte / 25 / Perris, CA mid-level rubyist that prefers merb spitresky.com twitter.com/spitresky github.com/scottmotte [email protected]

Leopard Install (0.9.7) mkdir -p /data/db wget http://downloads.mongodb.org/osx/mongodbosx-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

Linux Install (0.9.7) mkdir -p /data/db wget http://downloads.mongodb.org/linux/mongodblinux-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-linuxx86_64-0.9.7 sudo cp -R /usr/local/mongodb-linux-x86_64-0.9.7/bin/* / usr/local/bin

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.ndOne()

Mongo or CouchMongodb (C++)drivers bson, document, schema-free Dynamic queries, indexing gridfs (needs an apache/nginx module) RAM Good at the web, faster development time Update in place (good for high update rates) master-master 50s kid

Couchdb (Erlang)REST json, document, schema-free map/reduce attachments http cache Good at the web, slower development time MVCC (fault tolerant, but requires compacting) replication indy kid

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

Mongodb ormsRuby mongo-ruby-driversudo gem install mongodb-mongo sudo gem install mongodb-mongo_ext (c extension)

Python mongo-python-drivereasy_install pymongo (c extension autoinstalled)

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

autumnhttp://autumn-orm.org/

mongorecordhttp://github.com/mongodb/mongo-activerecord-ruby

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

mongomapperhttp://github.com/jnunemaker/mongomapper

MongoMappersudo gem install mongomapper cong.gem 'jnunemaker-mongomapper' #rails dependency 'jnunemaker-mongomapper' #merb

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

Controllerclass Books < Application def index @books = Book.all display @books end def show(id) @book = Book.nd(id) raise NotFound unless @book display @book end ...

Validationsclass Book include MongoMapper::Document key :title, String key :description, String validates_presence_of :title #validates_numericality_of #validates_length_of #validates_format_of #more end

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

Callbacksclass Book .. key :description, String before_save :append_signature def append_signature self.description @user.id) #fast

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

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

Embedded Docs cont.class Reviews < Application .. def create(review) @ight = Flight.nd(params['ight_id']) @review = Review.new(review) if @review.valid? && @ight.reviews {:notice => "Review made"} else message[:error] = "Review fail" render :new end end end # in router.rb resources :ights, :identify => :id do resources :reviews, :identify => :uuid end # /ights/:ight_id/comments/new

Additional info created_at and updated_at are includedautomatically by MongoMapper

_id cannot currently be set withlike in couchrest.

MongoMapper like it can in Couchrest

cannot currently do @doc[custom_eld] indexing: @doc.ensure_index :login

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