Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

3,299 views

Published on

- Traditional Relational Model
- MongoDB Document Model
- Associating Entities in MongoDB

Published in: Data & Analytics
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)

  1. 1. Schema Design Sr. Solutions Architect, MongoDB, Inc. Jake Angerman
  2. 2. Agenda •  Traditional Relational Model •  MongoDB Document Model •  Associating Entities in MongoDB •  There will be a test at the end
  3. 3. Relational Model
  4. 4. Relational ModelThis is hard… Long time to develop Queries are complex Difficult to change
  5. 5. What happens when the requirements change?
  6. 6. Hard to make changes New Table New Table New Column Name Age Phone Email New Column
  7. 7. Have to Manage Changes in 3 Places Application Code Relational Database Object Relational Mapping XML Config DB Schema
  8. 8. Relational Model
  9. 9. Developers can be more productive Application Code
  10. 10. Developers can be more productive Application Code Rich Queries Geospatial Text Search Map Reduce Aggregation
  11. 11. The Document Model
  12. 12. Key → Value store •  One-dimensional storage •  Single value is a blob •  Query on key only •  No schema •  Value can be replaced but not updated Key Blob
  13. 13. Relational Record •  Two-dimensional storage •  Field contains a single value •  Query on any field •  Very structured schema •  Poor data locality requires many tables, joins,and indexes. Primary Key
  14. 14. MongoDB Document _id •  N-dimensional storage •  Field can contain many values and embedded values •  Query on any field & level •  Flexible schema •  Optimal data locality requires fewer indexes and provides better performance
  15. 15. Documents are easier Relational MongoDB { ! first_name: "Paul",! surname: "Miller"! city: "London",! location: [45.123,47.232],! cars: [ ! { model: "Bentley",! year: 1973,! value: 100000, … },! { model: "Rolls Royce",! year: 1965,! value: 330000, … }! }! }!
  16. 16. Terminology MongoDB RDBMS Document Row Collection Table Index Index Embedded Document Join Reference Foreign Key
  17. 17. Focus on data storage Focus on data use
  18. 18. What answers do I have? What questions do I have?
  19. 19. Modeling Data with MongoDB business card example
  20. 20. Business Card Contact Address
  21. 21. Referencing Addresses { _id : , street : , city : , state : ”, zip_code : , country : } Contacts { _id : , name : , title : , company : ”, phone : , address_id : }
  22. 22. Embedding Contacts { _id : , name : , title : , company : , address : { street : , city : , state : , zip_code : , country : }, phone : }
  23. 23. Schema Flexibility – different shapes { _id: , name : , title : , company : , address : { street : , city : , state : , zip_code : }, phone : } { _id : , name : , url : , title : , company : , email : , address : { street : , city : , state : , zip_code : } phone : , fax }
  24. 24. { “_id”: , “name”: , “title”: , “company”: , “address”: [ { “street”: , “city”: , “state”: , “zip_code”: , “country”: }, { “street”: , “city”: , “state”: } ], “phone”: } Schema Flexibility – easily changed { _id : , name : , title : , company : , address : { street : , city : , state : , zip_code : , country : }, phone : }
  25. 25. Modeling Data with MongoDB address book example
  26. 26. Address Book Entity-Relationship Contacts •  name •  company •  title Addresses •  type •  street •  city •  state •  zip_code Phones •  type •  number Emails •  type •  address Thumbnails •  mime_type •  data Portraits •  mime_type •  data Groups •  name N 1 N 1 N N N 1 1 1 11 Twitter •  name •  location •  web •  bio 1 1
  27. 27. One-to-One Contacts •  name •  company •  title Addresses •  type •  street •  city •  state •  zip_code Phones •  type •  number Emails •  type •  address Thumbnails •  mime_type •  data Portraits •  mime_type •  data Groups •  name N 1 N 1 N N N 1 1 1 11 Twitter •  name •  location •  web •  bio 1 1
  28. 28. contact •  twitter_id twitter1 1 Contact •  twitter twitter 1 Schema Design Choices One-to-One
  29. 29. Contact •  twitter twitter 1 You can query or index on embedded fields. For example: db.contacts.find( {"twitter.account":"@Decameron"} ) General Recommendation One-to-One { _id : 1375-12-21, name : "Giovanni Boccaccio", twitter : { account : "@Decameron", page : "https://twitter.com/RealBoccaccio" } }
  30. 30. One-to-Many Contacts •  name •  company •  title Addresses •  type •  street •  city •  state •  zip_code Phones •  type •  number Emails •  type •  address Thumbnails •  mime_type •  data Portraits •  mime_type •  data Groups •  name N 1 N 1 N N N 1 1 1 11 Twitters •  name •  location •  web •  bio 1 1
  31. 31. contact •  phone_ids: [ ] phone1 N Contact •  phones phone N Schema Design Choices One-to-Many
  32. 32. Contact •  phones phone N General Recommendation One-to-Many Few { _id : 1375-12-21, name : "Giovanni Boccaccio", twitter : { account : "@Decameron", page : "https://twitter.com/RealBoccaccio" } phone : [ work : "+39 0571-669811", home : "+39 671-946726", mobile : "+39 671-038747" ] }
  33. 33. Contacts •  name •  company •  title Addresses •  type •  street •  city •  state •  zip_code Phones •  type •  number Emails •  type •  address Thumbnails •  mime_type •  data Portraits •  mime_type •  data Groups •  name N 1 N 1 N N N 1 1 1 11 Twitters •  name •  location •  web •  bio 1 1 Many-to-Many
  34. 34. Contacts •  name •  company •  title •  phone Groups •  name GroupContacts •  group_id •  contact_id Use arrays instead X Traditional Relational Association Join Table Many-to-Many
  35. 35. group •  contact_ids: [ ] contactN N group contact •  group_ids: [ ]N N Duplicated data must be updated for consistency group •  contacts contact N contact •  groups group N Schema Design Choices Many-to-Many Reference Embed
  36. 36. group contact •  group_ids: [ ]N N Many-to-Many •  Use case: address book •  Contact references groups Reference
  37. 37. group •  contacts contact N Many-to-Many •  Use case: corporate email system •  Group embeds contacts for performance Embed
  38. 38. Contacts •  name •  company •  title addresses •  type •  street •  city •  state •  zip_code phones •  type •  number emails •  type •  address thumbnail •  mime_type •  data Portraits •  mime_type •  data Groups •  name N 1 N 1 twitter •  name •  location •  web •  bio N N N 1 1 Document model - holistic and efficient representation
  39. 39. Contact document example { “name” : “Gary J. Murakami, Ph.D.”, “company” : “MongoDB, Inc.”, “title” : “Lead Engineer”, “twitter” : { “name” : “Gary Murakami”, “location” : “New Providence, NJ”, “web” : “http://www.nobell.org” }, “portrait_id” : 1, “addresses” : , “phones” : , “emails” : }
  40. 40. Free training at university.mongodb.com
  41. 41. Download at mongodb.org/downloads
  42. 42. 25% off discount code: JakeAngerman

×