Document Database Design: Multi-level categories (MongoDB specifically) Document Database Design: Multi-level categories (MongoDB specifically) mongodb mongodb

Document Database Design: Multi-level categories (MongoDB specifically)


The best way to design any non-relational database is based on the queries you need to run against the data, not the data itself.

You can design a non-relational database any way you want, because there are no rules of normalization as there are with relational databases.


Re your comment:

You need to enumerate all the ways the data will be queried. Then decide what document structure would make the query most convenient in each case.

From this, some patterns may emerge. Then use your judgment to decide which document structure to pick, that satisfies the greatest cross-section of queries.

Also keep in mind that in MongoDB, storing data redundantly is just fine. Recommended, in fact, because it's unlikely that one single document structure will be right for all your queries. You might find one document structure is pretty good for most of your queries, but makes the last query impossible. That's when you should create a secondary, redundant document collection to serve that last query, because all the other queries are taken care of.

There are no rules for structuring non-relational databases. This makes them harder than relational databases. Sorry, but NoSQL is a case of TANSTAAFL!