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 DesignMike O’BrienSoftware Engineer, 10gen
Agenda• Working with documents• Basic Strategies• Modeling Relationships  One-to-One  One-to-Many  Many-to-Many           ...
RDBMS                MongoDBDatabase      ➜ DatabaseTable         ➜ CollectionRow           ➜ DocumentIndex         ➜ Inde...
Working withDocuments
Modeling Data
DocumentsProvide flexibility andperformance
Normalized Data
De-Normalized (embedded)Data
Relational Schema DesignFocus on data storage
Document Schema DesignFocus on data use
Schema DesignConsiderations• How do we manipulate the data?   – Dynamic Ad-Hoc Queries   – Atomic Updates   – Map Reduce• ...
Data Manipulation• Query Selectors  – Scalar: $ne, $mod, $exists, $type, $lt, $lte, $gt, $gte  – Vector: $in, $nin, $all, ...
Data Access• Flexible Schemas• Ability to embed complex data structures• Secondary Indexes• Multi-Key Indexes• Aggregation...
Getting Started
Library ManagementApplication• Patrons• Books• Authors• Publishers
An ExampleOne to One Relations
Modeling Patrons                             patron = {patron = {                     _id: "joe",  _id: "joe",            ...
One to One Relations• Mostly the same as the relational approach• Generally good idea to embed “contains” relationships• D...
An ExampleOne To Many Relations
Modeling Patronspatron = {  _id: "joe",  name: "Joe Bookreader",  join_date: ISODate("2011-10-15"),  addresses: [    {stre...
Publishers and Books• Publishers put out many books• Books have one publisher
BookMongoDB: The Definitive Guide,By Kristina Chodorow and Mike DirolfPublished: 9/24/2010Pages: 216Language: EnglishPubli...
Modeling Books – EmbeddedPublisherbook = {  title: "MongoDB: The Definitive Guide",  authors: [ "Kristina Chodorow", "Mike...
Modeling Books & PublisherRelationshippublisher = {  name: "O’Reilly Media",  founded: "1980",  location: "CA"}book = {  t...
Publisher _id as a ForeignKeypublisher = {  _id: "oreilly",  name: "O’Reilly Media",  founded: "1980",  location: "CA"}boo...
Book _id as a Foreign Keypublisher = {  name: "O’Reilly Media",  founded: "1980",  location: "CA"  books: [ "123456789", ....
Where Do You Put the ForeignKey?• Array of books inside of publisher   – Makes sense when many means a handful of items   ...
Another ExampleOne to Many Relations
Books and Patrons• Book can be checked out by one Patron at a time• Patrons can check out many books (but not 1000’s)
Modeling Checkoutspatron = {  _id: "joe",  name: "Joe Bookreader",  join_date: ISODate("2011-10-15"),  address: { ... }}bo...
Modeling Checkoutspatron = {  _id: "joe",  name: "Joe Bookreader",  join_date: ISODate("2011-10-15"),  address: { ... },  ...
DenormalizationProvides data locality           De-normalize for speed
Modeling Checkouts: Denormalizedpatron = {  _id: "joe",  name: "Joe Bookreader",  join_date: ISODate("2011-10-15"),  addre...
Referencing vs. Embedding• Embedding is a bit like pre-joined data• Document-level ops are easy for server to handle• Embe...
An ExampleSingle Table Inheritance
Single Table Inheritancebook = {  title: "MongoDB: The Definitive Guide",  authors: [ "Kristina Chodorow", "Mike Dirolf" ]...
An ExampleMany to Many Relations
Relational Approach
Books and Authorsbook = {  title: "MongoDB: The Definitive Guide",  authors = [      { _id: "kchodorow", name: "K-Awesome"...
An ExampleTrees
Parent Linksbook = {  title: "MongoDB: The Definitive Guide",  authors: [ "Kristina Chodorow", "Mike Dirolf" ],  published...
Child Linksbook = {  _id: 123456789,  title: "MongoDB: The Definitive Guide",  authors: [ "Kristina Chodorow", "Mike Dirol...
Modeling Trees• Parent Links  - Each node is stored as a document  - Contains the id of the parent• Child Links  - Each no...
Array of Ancestorsbook = {  title: "MongoDB: The Definitive Guide",  authors: [ "Kristina Chodorow", "Mike Dirolf" ],  pub...
An ExampleQueues
Book Documentbook = {  _id: 123456789,  title: "MongoDB: The Definitive Guide",  authors: [ "Kristina Chodorow", "Mike Dir...
Thank YouMike O’BrienSoftware Engineer, 10gen
Upcoming SlideShare
Loading in …5
×

Schema Design

7,996 views

Published on

MongoDB’s basic unit of storage is a document. Documents can represent rich, schema-free data structures, meaning that we have several viable alternatives to the normalized, relational model. In this talk, we’ll discuss the tradeoff of various data modeling strategies in MongoDB using a library as a sample application. You will learn how to work with documents, evolve your schema, and common schema design patterns.

Published in: Technology
  • Nice !! Download 100 % Free Ebooks, PPts, Study Notes, Novels, etc @ https://www.ThesisScientist.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • For data visualization,data analytics,data intelligence and ERP Tools, online training with job placements, register at http://www.todaycourses.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • I would have liked to see more example of how to make the needed queries on these schemas. It isn't always obvious!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Schema Design

  1. 1. Schema DesignMike O’BrienSoftware Engineer, 10gen
  2. 2. Agenda• Working with documents• Basic Strategies• Modeling Relationships One-to-One One-to-Many Many-to-Many Single Table En
  3. 3. RDBMS MongoDBDatabase ➜ DatabaseTable ➜ CollectionRow ➜ DocumentIndex ➜ IndexJoin ➜ Embedded DocumentForeign Key ➜ ReferenceTerminology
  4. 4. Working withDocuments
  5. 5. Modeling Data
  6. 6. DocumentsProvide flexibility andperformance
  7. 7. Normalized Data
  8. 8. De-Normalized (embedded)Data
  9. 9. Relational Schema DesignFocus on data storage
  10. 10. Document Schema DesignFocus on data use
  11. 11. Schema DesignConsiderations• How do we manipulate the data? – Dynamic Ad-Hoc Queries – Atomic Updates – Map Reduce• What are the access patterns of the application? – Read/Write Ratio – Types of Queries / Updates – Data life-cycle and growth rate
  12. 12. Data Manipulation• Query Selectors – Scalar: $ne, $mod, $exists, $type, $lt, $lte, $gt, $gte – Vector: $in, $nin, $all, $size• Atomic Update Operators – Scalar: $inc, $set, $unset – Vector: $push, $pop, $pull, $pushAll, $pullAll, $addToSet
  13. 13. Data Access• Flexible Schemas• Ability to embed complex data structures• Secondary Indexes• Multi-Key Indexes• Aggregation Framework – $project, $match, $limit, $skip, $sort, $group, $unwind• No Joins
  14. 14. Getting Started
  15. 15. Library ManagementApplication• Patrons• Books• Authors• Publishers
  16. 16. An ExampleOne to One Relations
  17. 17. Modeling Patrons patron = {patron = { _id: "joe", _id: "joe", name: "Joe Bookreader", name: "Joe Bookreader” address: {} street: "123 Fake St. ", city: "Faketon",address = { state: "MA", patron_id = "joe", zip: 12345 street: "123 Fake St. ", } city: "Faketon", } state: "MA", zip: 12345}
  18. 18. One to One Relations• Mostly the same as the relational approach• Generally good idea to embed “contains” relationships• Document model provides a holistic representation of objects
  19. 19. An ExampleOne To Many Relations
  20. 20. Modeling Patronspatron = { _id: "joe", name: "Joe Bookreader", join_date: ISODate("2011-10-15"), addresses: [ {street: "1 Vernon St.", city: "Newton", state: "MA", …}, {street: "52 Main St.", city: "Boston", state: "MA", …} ]}
  21. 21. Publishers and Books• Publishers put out many books• Books have one publisher
  22. 22. BookMongoDB: The Definitive Guide,By Kristina Chodorow and Mike DirolfPublished: 9/24/2010Pages: 216Language: EnglishPublisher: O’Reilly Media, CA
  23. 23. Modeling Books – EmbeddedPublisherbook = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", publisher: { name: "O’Reilly Media", founded: "1980", location: "CA" }}
  24. 24. Modeling Books & PublisherRelationshippublisher = { name: "O’Reilly Media", founded: "1980", location: "CA"}book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English"}
  25. 25. Publisher _id as a ForeignKeypublisher = { _id: "oreilly", name: "O’Reilly Media", founded: "1980", location: "CA"}book = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", publisher_id: "oreilly"}
  26. 26. Book _id as a Foreign Keypublisher = { name: "O’Reilly Media", founded: "1980", location: "CA" books: [ "123456789", ... ]}book = { _id: "123456789", title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English"}
  27. 27. Where Do You Put the ForeignKey?• Array of books inside of publisher – Makes sense when many means a handful of items – Useful when items have bound on potential growth• Reference to single publisher on books – Useful when items have unbounded growth (unlimited # of books)• SQL doesn’t give you a choice, no arrays
  28. 28. Another ExampleOne to Many Relations
  29. 29. Books and Patrons• Book can be checked out by one Patron at a time• Patrons can check out many books (but not 1000’s)
  30. 30. Modeling Checkoutspatron = { _id: "joe", name: "Joe Bookreader", join_date: ISODate("2011-10-15"), address: { ... }}book = { _id: "123456789", title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], ...}
  31. 31. Modeling Checkoutspatron = { _id: "joe", name: "Joe Bookreader", join_date: ISODate("2011-10-15"), address: { ... }, checked_out: [ { _id: "123456789", checked_out: "2012-10-15" }, { _id: "987654321", checked_out: "2012-09-12" }, ... ]}
  32. 32. DenormalizationProvides data locality De-normalize for speed
  33. 33. Modeling Checkouts: Denormalizedpatron = { _id: "joe", name: "Joe Bookreader", join_date: ISODate("2011-10-15"), address: { ... }, checked_out: [ { _id: "123456789", title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], checked_out: ISODate("2012-10-15") }, { _id: "987654321" title: "MongoDB: The Scaling Adventure", ... }, ... ]}
  34. 34. Referencing vs. Embedding• Embedding is a bit like pre-joined data• Document-level ops are easy for server to handle• Embed when the many objects always appear with (i.e. viewed in the context of) their parent• Reference when you need more flexibility
  35. 35. An ExampleSingle Table Inheritance
  36. 36. Single Table Inheritancebook = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), kind: "loanable", locations: [ ... ], pages: 216, language: "English", publisher: { name: "O’Reilly Media", founded: "1980", location: "CA" }}
  37. 37. An ExampleMany to Many Relations
  38. 38. Relational Approach
  39. 39. Books and Authorsbook = { title: "MongoDB: The Definitive Guide", authors = [ { _id: "kchodorow", name: "K-Awesome" }, { _id: "mdirolf", name: "Batman Mike" }, ] published_date: ISODate("2010-09-24"), pages: 216, language: "English"}author = { _id: "kchodorow", name: "Kristina Chodorow", hometown: "New York"}
  40. 40. An ExampleTrees
  41. 41. Parent Linksbook = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", category: "MongoDB"}category = { _id: MongoDB, parent: "Databases" }category = { _id: Databases, parent: "Programming" }
  42. 42. Child Linksbook = { _id: 123456789, title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English"}category = { _id: MongoDB, children: [ 123456789, … ] }category = { _id: Databases, children: ["MongoDB", "Postgres"}category = { _id: Programming, children: ["DB", "Languages"] }
  43. 43. Modeling Trees• Parent Links - Each node is stored as a document - Contains the id of the parent• Child Links - Each node contains the id’s of the children - Can support graphs (multiple parents / child)
  44. 44. Array of Ancestorsbook = { title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", categories: ["Programming", "Databases", "MongoDB” ]}book = { title: "MySQL: The Definitive Guide", authors: [ "Michael Kofler" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", parent: "MongoDB", ancestors: [ "Programming", "Databases", "MongoDB"]}
  45. 45. An ExampleQueues
  46. 46. Book Documentbook = { _id: 123456789, title: "MongoDB: The Definitive Guide", authors: [ "Kristina Chodorow", "Mike Dirolf" ], published_date: ISODate("2010-09-24"), pages: 216, language: "English", available: 3}db.books.findAndModify({ query: { _id: 123456789, available: { "$gt": 0 } }, update: { $inc: { available: -1 } }})
  47. 47. Thank YouMike O’BrienSoftware Engineer, 10gen

×