MongoDB Data Architecture — Simple Tip
MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time.
In this story, I’d like to give you a tip of how to architect your data structure correctly on your first try. This tip will save you hours / days / or even weeks of refactoring your code base and migrating your data.
Do you want one, or do you want many?
When making architectural decisions regarding what data structures should be used to store your data, place Arrays at the top of those decisions.
As an example, imagine a Lunch collection. For lunch, you could just have one dessert…
Or even better, you could have as many as you like!
Designing around an Array of desserts instead of just a single one, will allow you to add as many as you want down the road. If you don’t do this on your first release, and then decide you want to later, you’ll have to refactor your code base and migrate your data, which as we all know can be a pain in the rear.
Now you have many, but be more descriptive!
There are so many desserts to choose from, sometimes you need to give more detail into which ones you really want to eat. For instance, what kind of cookie are you eating? Chocolate chip, oatmeal, or raisen? What flavor of ice cream did you bring? So architect your data like that as well.
Adding this additional level of specifications to your data models will allow you to easily add more complex features to your applications without the need of refactoring your data structures and migrating your data. Without this tip, you may end up looking like Cookie Monster.
Hope this helped 👍🏻