using mongodb and a relational database at mongodb day
DESCRIPTION
Talk given at MongoDB Day in Austin about how to make MongoDB and a relational database work together in your application.TRANSCRIPT
Building Good RelationsUsing MongoDB and a relational database in your apps
Hayes Davis@hayesdavis
CheapTweet.com
If you’ve already got an app
You probably already have a database
It’s probably relational
http://www.mbari.org/ssds/ReferenceDocuments/RDB_ER.gif
It’s probably gotten a little
bit big
http://www.neonlite.ca/archives/images/big%20cat.jpg
Or maybe it’s gotten huge
http://www.fahad.com/pics/liger.jpg
And it’s probably pretty complicated
http://www.mazes.org.uk/i/101005m.gif
Or maybe really
complicated
http://tommilsom.com/wp-content/files/france/maze.jpg
Which makes it harder to change
http://twitter.com/dacort/status/11023722831
But, you’re not going to throw
all that out
http://blog.makezine.com/284163191_6f09179853_o.jpg
So let’s make NoSQL and SQL live in harmony
http://curiousanimals.net/wp-content/uploads/2008/03/cat-and-dog-sleep.jpg
But first...
Do you even need to?
We all want to play with shiny
new toys
http://actionfan.files.wordpress.com/2009/07/voltron_metallic-vinyl-edit.jpg
But sometimes that’s not the
best plan
http://www.pwn3d.us/wp-content/uploads/2006/11/voltron_1.jpg
So, give yourself a test
#1Is my data relational?
#1Are my data relational?
#2 Do my data access
patterns lend themselves to denormaliation?
#3Do I expect a lot of
schema change?
#4Can I drop ACID?
(Atomicity, Consistency, Isolation, Durability)
#5Do I want to support a
new piece of infrastructure?
If you answered yes to any of these questions...
... at least for parts of your app...
... then MongoDB might be
right for you
http://www.rankopedia.com/CandidatePix/25781.gif
Where to start...
Utilities and supporting tools
Logging & analysis
http://sbadrinath.files.wordpress.com/2009/03/different26rqcu3.jpg
We built Parrot
http://filmfanatic.org/reviews/wp-content/uploads/2008/01/anfscd-parrot.png
Isolated subsystems
http://themarkvolta.files.wordpress.com/2009/01/lost-map.jpg
Comments might be a good choice
http://www.readwriteweb.com/archives/facebook_wants_to_be_your_one_true_login.php
Getting practical(in Rails, at least)
Using MongoMapper and ActiveRecord
Getting set up
config/mongodb.yml
development: host: 127.0.0.1
test: host: 127.0.0.1
production: host: mongo-server.local
config/initializers/mongo.rb# Read in the config infomongo_cfg = YAML.load( IO.read("#{RAILS_ROOT}/config/mongodb.yml"))[RAILS_ENV]
# Create the connection from mongodb.ymlargs = [mongo_cfg['host'],mongo_cfg['port']].compactMongoMapper.connection = Mongo::Connection.new(*conn_args)
# Pick the Mongo databasedb_cfg = Rails.configuration.database_configuration[RAILS_ENV]MongoMapper.database = mongo_cfg['database'] || db_cfg['database']
Mixing your MM with your AR
http://i.treehugger.com/images/2007/10/24/frankenstein-jj-001.jpg
app/models/preferences.rb
class Preferences include MongoMapper::Document key :user_id, Integer end
apps/models/user.rb
class User < ActiveRecord::Base after_save :save_prefs def preferences @preferences ||= Preferences.find_or_create_by_user_id(id) end private def save_prefs @preferences.save if @preferences end end
Usage
# Set any preferences you likeuser = User.find_by_login('thatguy')user.preferences[:foreground] = '#FF0000'user.preferences[:foo] = 'bar'user.save
# Later on, you can do thisuser = User.find_by_login('thatguy')user.preferences[:foreground] #returns '#FF0000'
Our general solution
active-expando(very, very alpha)
Any attribute, any time# Grab a useruser = User.find(1)# Now set an attribute that didn't exist beforeuser.expandos.foreground = '#FF0000'# foreground is saved in Mongouser.save
# Find any users with a red foreground colorusers = User.expando_all( :conditions=>{:foreground=>'#FF0000'})
Skip expandos, use delegate
class Post < ActiveRecord::Base expando_config do delegate :tags endend
p = Post.find(1)p.tags = ['foo','bar']p.save
# Find any Post with the tag "foo"Post.expando_all(:conditions=>{'tags'=>'foo'})
It’s a rails plugin
It’s on GitHub(http://github.com/hayesdavis/active-expando)
Other issues
Where we’re going we don’t need migrations
(or do we?)http://www.gordtep.com/files/2009/08/bttf2.jpg
Flexibility is great(until it bites you)
A general word of caution
MongoDB is a moving target
So are the tools
There will be bugs
http://nitishkrishna.files.wordpress.com/2009/07/2007_there_will_be_blood_013.jpg
So, be ready to upgrade
Questions?