MongoDB
Ruby friendly document storage that doesn’t rhyme with ouch.




                                                D...
Wynn Netherland
     @pengwynn

   http://squeejee.com
http://locomotivation.com
Not every data problem looks like this
So why do so many databases look like this?
and this?
When are RDBMS less than ideal?
Your data is stored and retrieved mainly by primary key,
without complex joins.

You have ...
Enter key value stores
Some of the players
Project Voldemort
Ringo
Scalaris
Kai
Dynomite
MemcacheDB
ThruDB
CouchDB
Cassandra
HBase
Hypertable
Tok...
Tokyo Cabinet:
Popular with Rubyists, Big in Japan
Go kick the tires
• Lightning fast
• Works best for at objects
• Tokyo Tyrant for network access


http://www.igvita.com/2...
More document-oriented solutions
CouchDB
Apache CouchDB is a distributed, fault-tolerant and
schema-free document-oriented database accessible
via a RESTfu...
Erlang + Javascript
Map + reduce
Very cool
Plenty o’ Ruby to go around
Ruby libraries for CouchDB
• CouchRest
• Basic model
• RelaxDB
• CouchPotato
• CouchFoo
• ActiveCouch
http://www.slideshar...
Throw out everything
you learned about DB design
SQL                                       CouchDB

            Prede ned, explicit schema                      Dynamic, im...
SQL                                       CouchDB

       Prede ned, explicit schema                      Dynamic, implici...
In walks Mongo
INTRO
INTRO


• Built   For Speed

• Document/Collection     Oriented

• Dynamic    Queries and Indexes

• Replication   and Fai...
GREAT FOR

• Websites

• Caching

• High   volume, low value

• High   scalability

• Storage   of program objects and json
NOT AS GREAT FOR


• Highly   transactional

• Ad-hoc    business intelligence

• Problems    requiring SQL
INSTALLATION


• mkdir   -p /data/db

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

• cp   -R /usr/local/patht...
DATABASE



• same   concept as mysql

• made   up of collections
COLLECTION

• Think   table, but with no schema

• For   grouping documents into smaller query sets (speed)

• Eachtop lev...
DOCUMENT


• Stored   in a collection, think record or row

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

• ...
STORAGE (BSON)

{ author: 'joe',
    created: Date('03-28-2009'),
    title: 'Yet another blog post',
    text: 'Here is t...
THAT LOOKS LIKE JSON

Where’s the Ruby?
QUERYING

• db.collection.find({‘first_name’: ‘John’})     # finds all Johns

• db.collection.find({‘first_name’: /^J/})     # ...
MORE QUERYING

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

• :fields   (like :select in active record)

• ...
HOW DO YOU
           USE IT WITH RUBY

• mongo-ruby-driver   http://github.com/mongodb/mongo-ruby-
 driver

• activerecor...
NUNEMAPPER
               (MONGOMAPPER)

• Mongo     is not MySQL

• DSL    for modeling domain should also teach you Mong...
FEATURES

• Typecasting                       • Create  and Update with
                                      single or mu...
EXAMPLE
class User
  include MongoMapper::Document
  key :name, String, :required => true, :length => 5..100
  key :email,...
RANDOM AWESOMENESS

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

• Upserts   db.collection.upd...
John Nunemaker
http://orderedlist.com
  http://railstips.org

         Links
http://www.10gen.com
 http://mongodb.com
Wynn Netherland
     @pengwynn

   http://squeejee.com
http://locomotivation.com
MongoDB - Ruby document store that doesn't rhyme with ouch
MongoDB - Ruby document store that doesn't rhyme with ouch
Upcoming SlideShare
Loading in...5
×

MongoDB - Ruby document store that doesn't rhyme with ouch

12,906

Published on

Overview of MongoDB at Dallas.rb Ruby group. Thanks to John Nunemaker for sharing slides!

Published in: Technology
0 Comments
46 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
12,906
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
447
Comments
0
Likes
46
Embeds 0
No embeds

No notes for slide
























  • High volume, lower value. No transactions. Drivers for several languages. All network based for speed (instead of REST).
    Uses BSON for storage of objects and for building queries. Binary-encoded serialization like JSON. Allows for some representations that JSON does not.
    BSON seems “blob-like” but mongo speaks BSON and can “reach in” to documents.
    Powerful query language, with query optimizer. You can drop down to javascript also.
    Supports master/slave.

  • Talk about harmony and how we are abusing mysql for key/value meta data on pages.
    Talk about capped collections and how they work like memcache. Also, how they are used for replication even.




  • _id’s can be any type, just have to be unique in collection


  • find always returns a cursor that you can keep iterating through




  • Don’t have to typecast with mongo as it “knows” types but form submissions are always strings so typecasting has its benefits.
    * note required
    * note length
    * note numeric
    * note index
  • multikey - automatically index arrays of object values
    ensure index on tags or _keywords


  • MongoDB - Ruby document store that doesn't rhyme with ouch

    1. 1. MongoDB Ruby friendly document storage that doesn’t rhyme with ouch. Dallas.rb
    2. 2. Wynn Netherland @pengwynn http://squeejee.com http://locomotivation.com
    3. 3. Not every data problem looks like this
    4. 4. So why do so many databases look like this?
    5. 5. and this?
    6. 6. When are RDBMS less than ideal? 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/
    7. 7. Enter key value stores
    8. 8. Some of the players Project Voldemort Ringo Scalaris Kai Dynomite MemcacheDB ThruDB CouchDB Cassandra HBase Hypertable Tokyo Cabinet/Tyrant http://www.metabrew.com/article/anti-rdbms-a-list-of-distributed-key-value-stores/
    9. 9. Tokyo Cabinet: Popular with Rubyists, Big in Japan
    10. 10. Go kick the tires • 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/
    11. 11. More document-oriented solutions
    12. 12. 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/
    13. 13. Erlang + Javascript
    14. 14. Map + reduce
    15. 15. Very cool
    16. 16. Plenty o’ Ruby to go around
    17. 17. Ruby libraries for CouchDB • CouchRest • Basic model • RelaxDB • CouchPotato • CouchFoo • ActiveCouch http://www.slideshare.net/brianthecoder/couchdb
    18. 18. Throw out everything you learned about DB design
    19. 19. 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
    20. 20. 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
    21. 21. In walks Mongo
    22. 22. INTRO
    23. 23. INTRO • Built For Speed • Document/Collection Oriented • Dynamic Queries and Indexes • Replication and Failover
    24. 24. GREAT FOR • Websites • Caching • High volume, low value • High scalability • Storage of program objects and json
    25. 25. NOT AS GREAT FOR • Highly transactional • Ad-hoc business intelligence • Problems requiring SQL
    26. 26. INSTALLATION • mkdir -p /data/db • download pre-built for OSX and unzip to /usr/local/ • cp -R /usr/local/pathtomongo/bin /usr/local/bin
    27. 27. DATABASE • same concept as mysql • made up of collections
    28. 28. COLLECTION • Think table, but with no schema • For grouping documents into smaller query sets (speed) • Eachtop level entity in your app would have its own collection (users, articles, etc.) • Indexable by one or more key
    29. 29. 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
    30. 30. 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
    31. 31. THAT LOOKS LIKE JSON Where’s the Ruby?
    32. 32. QUERYING • db.collection.find({‘first_name’: ‘John’}) # finds all Johns • db.collection.find({‘first_name’: /^J/}) # regex • db.collection.find_first({‘_id’:1}) # finds first with _id of 1 • db.collection.find({‘age’: {‘$gt’: 21}}) # finds possible drinkers • db.collection.find({‘author.first_name’:‘John’}) # subdocument • db.collection.find({$where:‘this.age >= 6 && this.age <= 18’})
    33. 33. MORE 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)
    34. 34. HOW DO YOU USE IT WITH RUBY • mongo-ruby-driver http://github.com/mongodb/mongo-ruby- driver • activerecord adapter http://github.com/mongodb/ activerecord-mongo-adapter • mongorecord http://github.com/mongodb/mongo- activerecord-ruby
    35. 35. NUNEMAPPER (MONGOMAPPER) • Mongo is not MySQL • DSL for modeling domain should also teach you Mongo • It sounded fun • Almost finished and sitting in private GitHub repo
    36. 36. FEATURES • Typecasting • Create and Update with single or multiple • Callbacks(ActiveSupport Callbacks) • Delete and Destroy and _all counterparts • Validations (using my fork of validatable) • Find: id, ids, :all, :first, :last • Connection and database • Associations (incomplete) can differ per document
    37. 37. 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::Document key :street, String key :city, String key :state, String, :length => 2 key :zip, Integer, :numeric => true, :length => 5 end
    38. 38. 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
    39. 39. John Nunemaker http://orderedlist.com http://railstips.org Links http://www.10gen.com http://mongodb.com
    40. 40. Wynn Netherland @pengwynn http://squeejee.com http://locomotivation.com
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×