Mastering Mongoose‘s chapter about schema design describes 3 principles for designing Mongoose schemas that scale to over 100k documents:
- The Principle of Least Cardinality
- The Principle of Denormalization
- The Principle of Data Locality
The Principle of Data Locality states that a document should store all the data you need to display a detail page for that document
. A concrete example is a blog post. I don’t think it is any surprise that NoSQL databases became popular shortly after the rise of blogging: loading the data to display a blog post in SQL quickly devolves into JOIN hell
because you need to join together comments, tags, authors, etc.
Instead, the Principle of Data Locality encourages you to store all relevant data in a single blog post document: authors, tags, comments, even related posts. There are several reasons for this. First is performance: most blog post documents will be loaded thousands of times for every time they’re modified. Ensuring all the data is in one place helps you reduce load on your database and minimize the number of queries you need.
Another reason is maintenance and debugging. Because the data in the database lines up so closely with what ends up on the user’s screen, it is easy to tweak the database to fix issues. Bugs happen, but their impact on users can be minimized if you can easily tweak the state of their data.