vs Denormalization ➤ Referencing/Linking vs Embedding vs *Bucketing ➤ Aggregation vs Computation ➤ Understanding relationships between Data and how they relate to the Document Model in MongoDB: ✴ One-To-One Relationships (1:1) ✴ One-To-Many Relationships (1:N) ✴ Many-To-Many Relationships (N:M) Data Modeling Basics
a document Used in Relational & Normalized sets of data. Popular in SQL databases Terms Restaurant Document Address Document Grades Document ⃪ Reference ⃪ Reference …
of a parent document. Usually used in Denormalized sets of data Terms Denormalized Restaurant Document ͢ address & grades documents embedded in Restaurant Document
where one side can have more than one relationship while the reverse relationship can only be single-sided Example: A Blog database where a Blog Post can have multiple comments but a Comment can only be related to one post
of a relationship between two documents where they both might have multiple relationships between each other Example: A Library database where a Book may be written by multiple Authors and an Author may have written multiple Books
a specific Author Fetch Authors of a specific Book In both cases, we have to perform queries in both directions. First to find either the Book or Author, and then performing an $in query to find the books or authors
Documents are mostly queried together ‣Documents are tightly coupled ‣Read Efficiency is required ‣Atomic updates are required ‣Referencing may lead to multiple inefficient Queries ‣Documents have multiple relationships ‣Write Efficiency is required ‣Documents may grow rapidly over time ‣Embedding may lead to excessive duplication
write operations that are atomic on the level of a single document • db.collection.update( ) • db.collection.findAndModify( ) • db.collection.remove( ) For fields that must be updated together, embedding the fields within the same document ensures that the fields can be updated atomically. Book Document 2
mongorestore, copydatabase) ‣ Model your Schema based on expected Queries ‣ Limit Database instance’s Network Exposure ‣ Use Authentication ❗(Everywhere) ‣ Again, Backup! ‣ Use untrusted apps to access your Database ‣ Use the same instance for development, testing & production ‣ Store sensitive data without encryption ‣ Create unnecessary User Accounts ‣ Agree that NoSQL is a fad
‣Handle ALL possible errors ‣Use push keys to ensure JSON key uniqueness ‣Validate data ‣Commit database keys to Source Control ‣Use untrusted apps to access database ‣Trust client-side connectivity ‣Send multiple queries for data (Denormalize) Remember: Data Duplication isn’t always a BAD thing. Storage is cheaper than Developer Time Efficiency is Key