Using MongoDB and a Relational Database at MongoDB Day

26,855 views
26,570 views

Published on

Talk given at MongoDB Day in Austin about how to make MongoDB and a relational database work together in your application.

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

No Downloads
Views
Total views
26,855
On SlideShare
0
From Embeds
0
Number of Embeds
16,757
Actions
Shares
0
Downloads
887
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Using MongoDB and a Relational Database at MongoDB Day

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

×