20. “We the Tweeple of the United
States, in order to form a more
perfect government, establish
communication, and promote
transparency do hereby tweet the
Congress of the United States of
America.”
21. and aggregator
^
The idea: A Twitter directory for the US Congress
22.
23. 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/
24. Key value stores
Project Voldemort
me ver y cool
Ringo
Scalaris
So space
Kai
ojects in this
Dynomite
MemcacheDB
pr
ThruDB
CouchDB
Cassandra
HBase
Hypertable
Redis
Tokyo Cabinet/Tyrant
Riak
Neo4J
27. 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/
28. SQL CouchDB
Prede ned, explicit schema Dynamic, implicit schema
Collection of named documents with varying
Uniform tables of data
structure
Normalized. Objects spread across tables. Denormalized. Docs usually self contained. Data
Duplication reduced. often duplicated.
Must know schema to read/write a complete
Must know only document name
object
Dynamic queries of static schemas Static queries of dynamic schemas
http://damienkatz.net/files/What is CouchDB.pdf
http://damienkatz.net/files/What is CouchDB.pdf
29. SQL CouchDB
Prede ned, explicit schema Dynamic, implicit schema
Collection of named documents with varying
Uniform tables of data
structure
Normalized. Objects spread across tables.
Duplication reduced.
TheDenormalized. Docs usually selfdetails
devil'soften duplicated.contained. Data
in the
Must know schema to read/write a complete
Must know only document name
object
Dynamic queries of static schemas Static queries of dynamic schemas
http://damienkatz.net/files/What is CouchDB.pdf
http://damienkatz.net/files/What is CouchDB.pdf
37. Introducing
★ Built For Speedcan be very fast
★ Dynamic Queries and Indexes
★ Replication and Failover
★ Sharding
★ Map / Reduce
★ Geospatial stuffs
38. MongoDB is great for
★ Websites
Your own queryable API mirror
★ Caching
★ High volume, low value
★ High scalability stash the hash
★ Storage of program objects and JSON
39. Not as great for
★ Highly transactional
★ Financial stuffs
★ Problems requiring SQL
40. Installation
★ mkdir -p /data/db
★ download pre-built for OSX and unzip to /usr/local/
★ cp -R /usr/local/pathtomongo/bin /usr/local/bin
Ruby driver for MongoDB
★ gem install mongo
Native C
★ gem install mongo_ext extensions ( go turbo! )
★ gem install mongo_mapper
42. Contents of mongo/bin what's in the box?
★ mongod - The MongoDB server
★ mongo - the JavaScript interactive shell
★ mongoexport - export data as JSON or csv
★ mongoimport - As advertised
was?
★ mongodump - Like mysqldump why? What did you think it
★ mongorestore - Restore from mongodump les
★ mongos - Auto-sharding module (getting better with every build)
47. Collections in MongoDB
★ Schema-less but typed!
★ 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
56. MongoMapper from @jnunemaker
★ Mongo is not MySQL
★ DSL for modeling domain should also teach you Mongo
★ Now in version 0.8.6
I voted for "Nunemapper"
57. 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
Be careful. Ordering can be tricky.
★ Find: id, ids, :all, : rst, :last
★ Associations
58. 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 :articles
end
Included as module, not subclassed
class Address
include MongoMapper::Document
key :street, String
key :city, String
key :state, String, :length => 2
key :zip, Integer, :numeric => true, :length => 5
end
60. Scopes
class User
include MongoMapper::Document
# plain old vanilla scopes with fancy queries
scope :johns, where(:name => 'John')
# plain old vanilla scopes with hashes
scope :bills, :name => 'Bill'
# dynamic scopes with parameters
scope :by_name, lambda { |name| where(:name => name) }
scope :by_ages, lambda { |low, high| where(:age.gte => low, :age.lte => high) }
# Yep, even plain old methods work as long as they return a query
def self.by_tag(tag)
where(:tags => tag)
end
# You can even make a method that returns a scope
def self.twenties; by_ages(20, 29) end
key :name, String
key :tags, Array
end
61. Scopes
# simple scopes
pp User.johns.first
pp User.bills.first
# scope with arg
pp User.by_name('Frank').first
# scope with two args
pp User.by_ages(20, 29).all
# chaining class methods on scopes
pp User.by_ages(20, 40).by_tag('ruby').all
# scope made using method that returns scope
pp User.twenties.all
64. Mongomatic
require 'mongomatic'
class User < Mongomatic::Base
def validate
self.errors.add "name", "can't be empty" if self["name"].blank?
self.errors.add "email", "can't be empty" if self["email"].blank?
end
end
# set the db for all models:
Mongomatic.db = Mongo::Connection.new.db("mongomatic_test")
# or you can set it for a specific model:
User.db = Mongo::Connection.new.db("mongomatic_test_user")
69. How can you help?
★ We need an awesome admin GUI
★ Port some plugins (might get easier with ActiveModel support
coming soon)
★ Build something cool
70. Lessons learned in production the fine print
★ The laws of computing are still in effect Upserts FTW!
★ Indexes are important no matter what the salesman told ya about
performance
★ Data modeling. Deep or Wide? The answer is yes!
★ MongoDB and MongoMapper are in active development
Very responsive yet very volatile changes!