Chad Tindel's presentation covered schema design considerations for MongoDB including working with documents, evolving schemas over time, queries and indexes, and common data modeling patterns. Some key points included embedding data for performance, using references when growth is unbounded, and modeling one-to-many relationships by either embedding the related data or storing foreign keys. The presentation provided examples of modeling patrons, books, publishers, and other data to demonstrate schema patterns for trees, inheritance, and other common relationships.
13. Schema Design
Considerations
• 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
20. 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
24. Book
MongoDB: The Definitive Guide,
By Kristina Chodorow and Mike Dirolf
Published: 9/24/2010
Pages: 216
Language: English
Publisher: O’Reilly Media, CA
28. Book _id as a Foreign Key
publisher = {
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"
}
29. Where do you put the foreign
Key?
• 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
36. 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 (viewed in the context of) their parents.
• Reference when you need more flexibility
42. 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)
In the filing cabinet model, the patient’s x-rays, checkups, and allergies are stored in separate drawers and pulled together (like an RDBMS)In the file folder model, we store all of the patient information in a single folder (like MongoDB)
Flexibility – Ability to represent rich data structuresPerformance – Benefit from data locality
Concrete example of typical blog in typical relational normalized form
Concrete example of typical blog in typical relational normalized form
Concrete example of typical blog using a document oriented de-normalized approach
Concrete example of typical blog in typical relational normalized form
Tools for data manipulation
Tools for data access
Slow to get address data every time you query for a user. Requires an extra operation.
Patron may have two addresses, in this case, you would need a separate table in a relation databaseWith MongoDB, you simply start storing the address field as an arrayOnly patrons which have multiple addresses could have this schema!No migration necessary! but Caution: Additional application logic required!
Publisher is repeated for every book, data duplication!
Publisher is better being a separate entity and having its own collection.
Now to create a relation between the two entities, you can choose to reference the publisher from the book document.This is similar to the relational approach for this very same problem.
OR: because we are using MongoDB and documents can have arrays you can choose to model the relation by creating and maintaining an array of books within each publisher entity.Careful with mutable, growing arrays. See next slide.
Costly for a small number of books because to get the publisher
And data locality provides speed
Book’s kind attribute could be local or loanableNote that we have locations for loanable books but not for localNote that these two separate schemas can co-exist (loanable books / local books are both books)
Note that we partially de-normalize here.To get books by a particular author: - get the author - get books that have that author id in array
Simple solution. The biggest problem with this approach is getting an entire subtree requires several query turnarounds to the database
It may also be good for storing graphs where a node has multiple parents.