Where Documents Fit, and Where They Don't
Documents fit data that is read together, holds lists and changes shape over time; a relational database is often the better choice when many records must change together, when the same data is combined in many ways, or when a strict shape is the point.
Apprentice · MongoDB · NoSQL · Database · Updated
After this lesson you can
- Explain when documents fit an app's data, such as records read together, lists inside a record, and a shape that changes over time
- Explain when a relational database is the better choice, such as records that change together, data combined many ways, or a strict shape
Key takeaways
- Documents fit data that's read together as one record, such as a recipe card shown whole on its page.
- Documents also fit records with lists inside them, and records whose shape changes over time, such as a new field on only some recipes.
- A relational database is often the better choice when many records must change together, such as an order that takes money and stock at once.
- It's also often better when the same data is combined in many different ways, or when one strict shape for every record is the point.
- Most apps could work either way, so choose for how the app reads and changes its data, and be ready to say why.
Sunday Kitchen is about to open a small shop on its website: pans, spice jars and gift boxes, with customers, orders and payments. Should all of that go in MongoDB, beside the recipes? "Which database?" comes up at the start of every project and in plenty of job interviews, and "it depends" is a good answer only when you can say what it depends on. This lesson gives you the reasons, both ways.
Where documents shine
Data that's read together. The recipe page shows the whole card at once: name, ingredients, steps, nutrition. As a document, that's one record, read in one go, with nothing to join. The same goes for a product page with its sizes and colors, or a user's profile with their settings.
Lists inside a record. A recipe's ingredients and steps, an order's items, a playlist's songs: each list belongs to one record and is read with it. A document keeps the list inside the record, where it's used, instead of in rows of another table.
A shape that changes over time. Sunday Kitchen starts adding air fryer times, but only to the recipes that suit an air fryer. In a table, every recipe gets a new column, mostly empty, and the table usually has to be changed before the first value goes in. In a collection, the new recipes have an airFryerMinutes field and the old ones don't, and both sit side by side. MongoDB doesn't make every document in a collection share one shape; the shape lives in your app. The glossary calls that schemaless: the database doesn't enforce one layout for every record, unless you ask it to.
Where a relational database is the better choice
Many records that must change together. When a customer buys two spice jars, the order is saved, the stock of each jar goes down, and the payment is recorded. If the server stops halfway, the stock mustn't drop for an order that was never saved. A transaction groups changes so they all happen or none do. Relational databases are built around transactions. MongoDB has them too (Module 3's Transactions, and Why You'll Need Them Less), and a change to one document is always all or nothing. But when almost every change your app makes spans many records, tables are often the better fit.
The same data combined in many ways. The shop's owner asks a new question every week: sales by spice and by month, customers who bought a pan but never a spice, the best day for gift boxes. Tables keep one copy of each fact, and a join combines any tables in whatever way a question needs, including questions nobody planned. Documents are shaped around the questions you planned for. MongoDB can bring documents from two collections together ($lookup: Bringing in Related Documents, in Module 4), but for questions that change weekly, a relational database is the more natural home.
When a strict shape is the point. In bookkeeping, every payment must have an amount, a date and an account, with no exceptions. A relational table refuses a row that doesn't fit, from the first day. MongoDB accepts any document unless you add rules (Schema Validation with $jsonSchema, in Module 3). That flexibility has a cost: a field name typed as minuts goes in without a word of warning, as Documents: Fields, Values, Quotes and Braces shows.
Choosing for Sunday Kitchen
Ask the same two questions of each part of the app: how is it read, and how does it change?
| Part of the app | How it's read and changed | The better fit, and why |
|---|---|---|
| Recipe cards | Shown whole on a page, with lists inside; new fields appear on some recipes | Documents: one read per page, and the shape can grow |
| A cook's profile and saved recipes | Read with the cook, changed by that cook alone | Documents: the profile and its list belong together |
| Orders and payments | Money and stock change together on every order | Relational, often: transactions are its everyday work |
| The owner's weekly sales questions | The same orders combined in new ways every week | Relational: joins answer questions nobody planned |
Plenty of real apps use both: Sunday Kitchen could keep its recipes in MongoDB and its orders in a relational database. Plenty of others run well on either one, because these reasons tip the balance rather than rule a choice out. What matters is that you choose for how the app uses its data, and can say why. Module 3's When a Relational Database Is the Better Choice goes deeper, with a whole store designed both ways.
Check yourself
2 questions to try on the lesson page.
So what
"Which database?" now has an answer you can explain: documents for records read together, with lists and a shape that grows; tables for records that change together, data combined many ways, and a shape that must never bend.