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.

Schema Design

1,807 views

Published on

Published in: Technology, Business

Schema Design

  1. 1. Schema Design Bob Grabar Technical Writer, MongoDB, Inc. bob@mongodb.com
  2. 2. Agenda • Core Concepts • Modeling Data • Modeling Relationships
  3. 3. Schema design is about your Application
  4. 4. Relational  Normalize
  5. 5. { MongoDB "patient_id": "1177099", "first_name": "John", "last_name": "Doe", "dob": "2000-01-25", "gender": "Male", "blood_type": "B+", "address": "123 Elm St., Chicago, IL 59923", "height": "66", "weight": "110", "allergies": ["Nuts", "Penicillin", "Pet Dander"], "current_medications": [{"name": "Zoloft", "dosage": "2mg", "frequency": "daily", "route": "orally"}], "complaint" : [{"entered": "2000-11-03", "onset": "2000-11-03", "prob_desc": "", "icd" : 250.00, "status" : "Active"}, {"entered": "2000-02-04", "onset": "2000-02-04", "prob_desc": "in spite of regular exercise...", "icd" : 401.9, "status" : "Active"}], "diagnosis" : [{"visit" : "2005-07-22" , "narrative" : "Fractured femur", "icd" : "9999", "priority" : "Primary"}, {"visit" : "2005-07-22" , "narrative" : "Type II Diabetes", "icd" : "250.00", "priority" : "Secondary"}] }
  6. 6. Terminology RDBMS MongoDB Table Collection Row Document Index Index Join Embedded Document Foreign Key Reference
  7. 7. Relational Record • Two-dimensional storage • Field can contain single value • Structured schema • Poor data locality primary key
  8. 8. MongoDB Document • Multi-dimensional storage • Field can contain many values • Flexible schema • Optimal data locality _id
  9. 9. Focus on data storage Focus on data use
  10. 10. What answers do I have? What questions do I have?
  11. 11. Modeling Data
  12. 12. Business Card
  13. 13. Contact Addres s
  14. 14. Referencing Contacts Addresses { { _id : , name : title : company : phone : address_id : } _id : , street : city : state : ", zip_code : country : , , ", , } , , ,
  15. 15. Embedding Contacts { _id : , name : title : company : phone : address : { street : city : state : , zip_code : country : } } , , ", , , , ,
  16. 16. Schema Flexibility Contacts { name : url : title : , company : email : address : { street : city : state : , zip_code : } phone : fax { name : title : company : phone : address : { street : city : state : , zip_code : } , , , , , } } , , , , , , ,
  17. 17. Schema Flexibility { { _id : , name : title : company : address : { street : city : state : , zip_code : country : }, phone : _id : , , name : title : company : address : [ { street : city : state : , zip_code : country : }, { street : city : state : zip_code : country : } ], phone : , , , , , } } , , , , , , , ,
  18. 18. Modeling Relationships
  19. 19. Address Book Entity-Relationship • • • • name location web bio • name N 1 N 1 1 Contacts Thumbnails • mime_type • data 1 • • • N • • type street city state zip_code Phones • name 1 • company • title 1 1 1 Portraits • mime_type • data Addresses Groups Twitters 1 N • type • number Emails N • type • address
  20. 20. One-to-One • • • • name location web bio • name N 1 N 1 1 Contacts Thumbnails • mime_type • data 1 • • • N • • type street city state zip_code Phones • name 1 • company • title 1 1 1 Portraits • mime_type • data Addresses Groups Twitters 1 N • type • number Emails N • type • address
  21. 21. One-to-One Schema Design Choices contact • twitter_id 1 1 twitter Contact • twitter twitter 1
  22. 22. One-to-One General Recommendation { _id : ObjectId("52ebde4e5f2b124a3a09eb8e"), name : "Giovanni Boccaccio", twitter : { name : "Gian", location : "Certaldo", web : "https://twitter.com/RealBoccaccio" } Contact • twitter twitter 1 } db.contacts.find( { "twitter.location" : "Certaldo" } )
  23. 23. One-to-Many • • • • name location web bio • name N 1 N 1 1 Contacts Thumbnails • mime_type • data 1 • • • N • • type street city state zip_code Phones • name 1 • company • title 1 1 1 Portraits • mime_type • data Addresses Groups Twitters 1 N • type • number Emails N • type • address
  24. 24. One-to-Many Schema Design Choices contact • phone_ids: [ ] 1 N phone Contact • phones phone N
  25. 25. One-to-Many Contact Book Application { _id : ObjectId("52ebde4e5f2b124a3a09eb8e"), name : "Giovanni Boccaccio", twitter : { name : "Gian", location : "Certaldo", web : "https://twitter.com/RealBoccaccio" }, phones : [ { type : "work" , number : "+39 0571-669811" }, { type : "home" , number : "+39 671-946726" }, { type : "mobile" , number : "+39 671-038747" } ] } Contact • phones phone N
  26. 26. Many-to-Many • • • • name location web bio • name N 1 N 1 1 Contacts Thumbnails • mime_type • data 1 • • • N • • type street city state zip_code Phones • name 1 • company • title 1 1 1 Portraits • mime_type • data Addresses Groups Twitters 1 N • type • number Emails N • type • address
  27. 27. Many-to-Many Traditional Relational Association Join Table Groups • name X GroupContacts • group_id • contact_id Contacts • • • • Use arrays instead name company title phone
  28. 28. Many-to-Many Schema Design Choices Reference Embed group • contact_ids: [ ] N N contact group • contacts contact group contact N N • group_ids: [ ] contact • groups N group N
  29. 29. Many-to-Many General Recommendation group contact N N • group_ids: [ ] Contact references groups
  30. 30. Many-to-Many Group embeds contacts for performance group • contacts contact N
  31. 31. Document model - holistic and efficient representation Groups Contacts • name N • name • company • title twitter N 1 1 Portraits • mime_type • data • • • • addresses N 1 name location web bio thumbnail 1 • mime_type • data • • • • • type street city state zip_code phones N • type • number emails • type • address N
  32. 32. 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” : }
  33. 33. Resources
  34. 34. mongodb.org/downloads
  35. 35. mongodb.com
  36. 36. Thank You Bob Grabar Technical Writer, MongoDB, Inc. bob@mongodb.com

×