Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoDB Grand Rapids

MongoDB Grand Rapids

This document provides an overview of MongoDB, including that it is a document-oriented database that is built for speed, supports dynamic queries and indexes, and replication and failover. It notes MongoDB is well-suited for websites, caching, storage of program objects and JSON, and high scalability. The document describes MongoDB concepts like databases, collections, documents, and querying. It also discusses options for using MongoDB with Ruby like the mongo-ruby driver and MongoMapper ORM.

John Nunemaker

June 02, 2009
Tweet

More Decks by John Nunemaker

Other Decks in Programming

Transcript

  1. INTRO • Built For Speed • Document/Collection Oriented • Dynamic

    Queries and Indexes • Replication and Failover
  2. GREAT FOR • Websites • Caching • High volume, low

    value • High scalability • Storage of program objects and json
  3. NOT AS GREAT FOR • Highly transactional • Ad-hoc business

    intelligence • Problems requiring SQL
  4. INSTALLATION • mkdir -p /data/db • download pre-built for OSX

    and unzip to /usr/local/ • cp -R /usr/local/pathtomongo/bin /usr/local/bin
  5. 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
  6. 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
  7. 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’})
  8. 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)
  9. 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
  10. 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
  11. 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)
  12. 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
  13. 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