Beruflich Dokumente
Kultur Dokumente
For Natural
Developemnt
@tommychheng
1
Thursday, February 25, 2010 1
Who uses it?
2
Thursday, February 25, 2010 2
Why?
• NoSQL trend?
• Scalability?
• Natural Development!
3
Thursday, February 25, 2010 3
Compare
SQL MongoDB
BSON Documents/
Data Model Rows/Tables
Collections
Primitives+Arrays/
Data Types Primitives
Hashes
4
Thursday, February 25, 2010 4
SQL Example
Documents Revisions
Tags
title author
name
age updated_at
document_id
document_id
5
Thursday, February 25, 2010 5
MongoDB Example
document = {title: 'A Tale of Two Cities',
age: 1,
revisions: [{author: “tommy”,
updated_at: new Date(’02-20-2012’)}],
tags: ['MongoDB','nosql']}
6
Thursday, February 25, 2010 6
Mongo Install
• http://www.mongodb.org/display/DOCS/
Quickstart
• Chef recipe available
• http://github.com/schisamo/
chef_cookbooks/
• Start server: ./mongod
• Run console: mongo
7
Thursday, February 25, 2010 7
How to use in Ruby?
8
Thursday, February 25, 2010 8
Mongo Help
• http://groups.google.com/group/mongodb-
user
• http://groups.google.com/group/
mongomapper
9
Thursday, February 25, 2010 9
MongoMapper Models
• No Migrations!
• AR Validations
• AR Callbacks
• Testing using Factory Girl
• Rails 3?
• fork which uses use ActiveModel, see
mailinglist
10
Thursday, February 25, 2010 10
MongoMapper Model
class Article
include MongoMapper::Document
key :profile_id, ObjectId, :required => true
key :title, String, required => true
key :age, Integer
class ArticleRevision
include MongoMapper:EmbeddedDocument
key :profile_id, ObjectId
timestamps!
end
11
Thursday, February 25, 2010 11
ActiveRecord-style Save
article = Article.new({:profile_id => p.id,
:title => “hello”,
:age => 1})
article.revisions <<
ArticleRevision.new({:profile_id => p.id,
:updated_at => Date.new(‘Jan 1 2012’)})
article.save
12
Thursday, February 25, 2010 12
ActiveRecord-style Querying
article = Article.find(‘4b85bb3fea04805c077e84e9’)
article = Article.first
13
Thursday, February 25, 2010 13
CouchDB or MongoDB?
CouchDB MongoDB
One big database Database has
Data Modeling
for all documents. many collections.
Interface HTTP REST sockets
Find by id
Find by id
Querying map/reduce JS
dynamic JS queries
functions
14
Thursday, February 25, 2010 14
Limitations
• Memory Mapped file:
• 32-bit 4GB limit, use 64-bit
• Atomic updates only at document level
• Solve by nesting related data in document
• MongoDB lacks:
• transactions
• CouchDB-style MVCC revisioning
• http://www.mongodb.org/display/DOCS/Atomic
15
Thursday, February 25, 2010 15
Author
• Blog http://tommy.chheng.com
• Twitter @tommychheng
• Side project: http://nextsprocket.com/
• using mongoDB + mongo_mapper!
16
Thursday, February 25, 2010 16