The document provides an overview of schema design considerations for MongoDB databases. It discusses modeling different types of relationships like one-to-one, one-to-many, and many-to-many. Examples are given for embedding vs referencing related data as well as techniques for trees, single table inheritance, and queues. Key points covered include evolving schemas based on data usage patterns, embedding for performance vs referencing for flexibility, and modeling relationships in MongoDB similarly to relational databases while leveraging embedding and references.
11. 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
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
22. Book
MongoDB: The Definitive Guide,
By Kristina Chodorow and Mike Dirolf
Published: 9/24/2010
Pages: 216
Language: English
Publisher: O’Reilly Media, CA
26. 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"
}
27. 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
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 (viewed in the context of) their parents.
• Reference when you need more flexibility
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)
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 using a document oriented de-normalized approach
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)
Kristine to update this graphic at some point
Authors often use pseudonyms for a book even though it’s the same individualTo get books by a particular author: - get the author - get books that have that author id in array