Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

INTRO

Slide 3

Slide 3 text

INTRO • Built For Speed • Document/Collection Oriented • Dynamic Queries and Indexes • Replication and Failover

Slide 4

Slide 4 text

GREAT FOR • Websites • Caching • High volume, low value • High scalability • Storage of program objects and json

Slide 5

Slide 5 text

NOT AS GREAT FOR • Highly transactional • Ad-hoc business intelligence • Problems requiring SQL

Slide 6

Slide 6 text

INSTALLATION • mkdir -p /data/db • download pre-built for OSX and unzip to /usr/local/ • cp -R /usr/local/pathtomongo/bin /usr/local/bin

Slide 7

Slide 7 text

DATABASE • same concept as mysql • made up of collections

Slide 8

Slide 8 text

COLLECTION • Think table, but with no schema • For grouping documents into smaller query sets (speed) • Each top level entity in your app would have its own collection (users, articles, etc.) • Indexable by one or more key

Slide 9

Slide 9 text

DOCUMENT • Stored in a collection, think record or row • Can have _id key that works like primary keys in MySQL • Two options for relationships: subdocument or db reference

Slide 10

Slide 10 text

QUERYING • db.collection. fi nd({‘ fi rst_name’: ‘John’}) # fi nds all Johns • db.collection. fi nd({‘ fi rst_name’: /^J/}) # regex • db.collection. fi nd_ fi rst({‘_id’:1}) # fi nds fi rst with _id of 1 • db.collection. fi nd({‘age’: {‘$gt’: 21}}) # fi nds possible drinkers • db.collection. fi nd({‘author. fi rst_name’:‘John’}) # subdocument • db.collection. fi nd({$where:‘this.age >= 6 && this.age <= 18’})

Slide 11

Slide 11 text

MORE QUERYING • $in, $nin, $all, $ne, $gt, $gte, $lt, $lte, $size, $where • : fi elds (like :select in active record) • :limit, :offset for pagination • :sort ascending or descending [[‘foo’, 1], [‘bar’, -1]] • count and group (uses map/reduce)

Slide 12

Slide 12 text

HOW DO YOU USE IT WITH RUBY • mongo-ruby-driver http://github.com/mongodb/mongo-ruby- driver • active record adapter http://github.com/mongodb/ activerecord-mongo-adapter • mongorecord http://github.com/mongodb/mongo- activerecord-ruby

Slide 13

Slide 13 text

NUNEMAPPER (MONGOMAPPER) • Mongo is not MySQL • DSL for modeling domain should also teach you Mongo • It sounded fun • Almost fi nished and sitting in private GitHub repo

Slide 14

Slide 14 text

FEATURES • Typecasting • Callbacks (ActiveSupport Callbacks) • Validations (using my fork of validatable) • Connection and database can differ per document • Create and Update with single or multiple • Delete and Destroy and _all counterparts • Find: id, ids, :all, : fi rst, :last • Associations (incomplete)

Slide 15

Slide 15 text

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 class Address include MongoMapper::EmbeddedDocument key :street, String key :city, String key :state, String, :length => 2 key :zip, Integer, :numeric => true, :length => 5 end

Slide 16

Slide 16 text

RANDOM AWESOMENESS • 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

Slide 17

Slide 17 text

John Nunemaker http://orderedlist.com http://railstips.org Links http://www.10gen.com http://mongodb.com