MongoDB

description »
Jim Mulholland

Mongo (from "humongous") is a high-performance, open source, schema-free document-oriented  database.
The character "Mongo" from Mel Brook's 1974 movie Blazing Saddles does not help either. 
As an aside, the actor who played Mongo, Alex Karras, also played the part of the Dad on that lovable '80s sit-com, Webster!
Why MongoDB?
10Gen, the creator of MongoDB, most likely felt the need to include the "humongous" part because there has been some controversy in regards to the word "mongo" having a derogatory meaning in multiple language (including English). 
So to pay homage to Mongo, the humongous gingerbread man, this presentation has literally been themed with his image.
Perfect blend of RDBMS and document storage systems
Schemaless
Dynamic queries
Indexes
Replication / Failover
Auto-sharding (currently in alpha)
FAST
Great For:
Web sites
Caching
Logging
Scalability
Storing large objects
Overview:
Not So Great For:
Highly transactional apps
Ad-hoc business reporting
Problems needing complex SQL to solve
The Basics
Same as RDBMS
Has many collections (tables)
Database
Collection
Same as RDBMS table
Group similar documents into smaller query sets (speed)
Can be indexed on 1 or more keys
Three ways to relate to other collections: embedded document, foreign keys, or db reference

Document
Stored inside a collection
Each document stores both the key and the value (like a hash in Ruby)

Storage (BSON)
BSON, which stands for "Binary JSON", is the data storage format for "documents" in Mongo. It allows for storage of datatypes that JSON does not allow such as a Date data format and a BinData data format. 
Querying
From: http://railstips.org/2009/6/3/what-if-a-key-value-store-mated-with-a-relational-database-system
# finds all Johns
db.collection.find({‘first_name’: ‘John’})
# finds all documents with first_name 
# starting with J using a regex
db.collection.find({‘first_name’: /^J/}) 
# finds first with _id of 1
db.collection.find_first({‘_id’:1})
# finds possible drinkers (age > 21)
db.collection.find({‘age’: {‘$gt’: 21}})
# searches in embedded document author for 
# author that has first name of John
db.collection.find({‘author.first_name’:‘John’})
# worse case scenario, or if you need "or" 
# queries you can drop down to JavaScript
db.collection.find({$where:‘this.age >= 6 && this.age <= 18’})
More Querying
:fields (like :select in ActiveRecord)
:limit, :offset for pagination
:sort ascending or descending
count and group (uses map/reduce)
$in, $nin, $ne, $all, $gt, $gte, $lt, $size, $where
Ruby
Language Support
C++
Java
Perl
PHP
Ruby
Python
SpiderMonkey Javascript
http://www.mongodb.org/display/DOCS/Drivers
Drivers / ORMs
Mongo Ruby Driver
Active Record Adapter
MongoRecord
MongoMapper
http://github.com/mongodb/mongo-ruby-driver/tree/master
http://github.com/mongodb/activerecord-mongo-adapter/tree/master
http://github.com/mongodb/mongo-activerecord-ruby/tree/master
http://github.com/jnunemaker/mongomapper/tree/master
Other Cool Stuff
Capped collections
Data ages out when the collection is full similar to MemCache
The data is automatically kept in the insertion order
Example:
Upserts
GridFS
Storage for large objects in MongoDB
Stores objects as chunks of data
Uses 2 collections to store data
'files' contains object metadata
'chunks' contains the binary chunks
"find_or_create" in ActiveRecord
Example:
myColl.update( { name: "Joe" }, { name: "Joe", age: 20 }, { upsert: true } );
db.createCollection("mycoll", {capped: true, size:100000})
MongoMapper
Typecasting
Callbacks
Validations
Per object connection
Associations
Find all, first, last
Custom IDs
Dynamic finders (e.g.  find_by_name)
Per object database
Create keys on the fly
MongoMapper Example
(table)
(row)
http://railstips.org/2009/6/27/mongomapper-the-rad-mongo-wrapper
> person = Person.create({  :first_name => 
:last_name => 'Mulholland',  
:twitter_screen_name => 'mully',  
:age => 0,  
:born_at => Time.mktime(2009, 8, 28, 1, 1),  
:active => true,  
:fav_colors => %w(red green blue)})

=> #<Person twitter_screen_name: mully, updated_at: Sat Aug 29 05:02:49 UTC 2009, _id: 537a06444a98b67900000012, notes: , first_name: Jim, fav_colors: redgreenblue, born_at: Fri Aug 28 06:01:00 UTC 2009, age: 0, last_name: Mulholland, active: true, created_at: Sat Aug 29 05:02:49 UTC 2009>
> person = Person.last
> person.fav_colors=> ["red", "green", "blue"]
> person["favorite_animal"] = "dog"
> person.save
> person.favorite_animal=> "dog"
> person["family"] = {:wife => "Wendy", :son => "Ben", :twins => "Sam and Nicole"}
> person.save
> person.family.class=> OrderedHash
> person.family["twins"]=> "Sam and Nicole"
Creates and Updates
Embedded Documents
> address = Address.new({    :address => "1 Main",     :city => "Houston",     :state => "TX",     :zip => "77002"})
> person.addresses << address> person.save
> person.addresses.first
=> #<Address city: Houston, zip: 77002, _id: 421477b04a98c3370000007b, address: 1 Main, state: TX>
> Person.find(:first, :conditions=>{"addresses.city" => "Houston"})
=> #<Person twitter_screen_name: mully, updated_at: Sat Aug 29 05:57:14 UTC 2009, _id: 421477b04a98c2b90000007b, first_name: Jim, fav_colors: redgreenblue, born_at: Fri Aug 28 06:01:00 UTC 2009, age: 18, last_name: Mulholland, active: true, created_at: Sat Aug 29 05:55:05 UTC 2009>
> posts = person.posts.build
=> #<Post updated_at: , _id: , content: , person_id: 421477b04a98c2b90000007b, created_at: >

> posts.content = "This is a post"
=> "This is a post"
> posts.save
> person.posts.first.content
=> "This is a post"
Validations
Foreign Key Relationships
> person2 = Person.new({  :first_name => 'Jim',
:twitter_screen_name => 'mully',  
:age => 'twelve',  
:born_at => Time.mktime(2009, 8, 28, 1, 1),  
:active => true,  
:fav_colors => %w(red green blue)})
> person2.save
=> false
> person2.errors
=> #<Validatable::Errors:0x1a51814 @errors={:age=>["must be a number"], :last_name=>["can't be empty"]}>
What is especially cool about this Mongo is that he comes with his own theme song that could be interpretted that Bonnie Tyler (the singer) is asking for a better database.
Isn’t there a white knight upon a fiery steed?
Late at night I toss and turn and dream of what I need
He’s gotta be strong
And he’s gotta be fast
And he’s gotta be fresh from the fight
He’s gotta be sure
And it’s gotta be soon
And he’s gotta be larger than life
But there is a good Mongo...
Compared to other DBs
"Go, Mongo, go"
"More heat, less foam"
Thanks
Jim Mulholland
Twitter: @mully
jim@squeejee.com
"Let's crash this party, big fella."

Loading comments...

Please log in to add your comment.

Report abuse

More presentations by Jim Mulholland

  • RedDirt2010_MongoDB

    Jim Mulholland on

    MongoDB presentation at Red Dirt Ruby Conf 2010; Jim Mulholland & John Taber

  • Floxee

    Jim Mulholland on

    Entrepreneur Camp

  • demo

    Jim Mulholland on

    description