Using NoSQL databases is slightly different than using primary relational databases like PostgreSQL. You have more freedom in data modeling of your documents cause each document of a collection can contain diversified properties. In larger projects, this advantage can lead to making a little mess in your data. Such as in the programming case, design patterns can help you organize your relations between documents and collections. There are many patterns that you can choose during data modeling in MongoDB, but in this article, you get to know about three of them.
When you have many documents mostly with the same properties, but they also have their specific attributes, this pattern can be useful for you. All you need to do is create one array property for all these characteristics and put them into as key-value objects. This solution will enable you to sort and search documents in an easier way and it will improve the performance. You can use it, eg., in an e-commerce project where you have a lot of similar products.
This kind of pattern, as the name suggests, makes a tree of dependencies between documents of a collection. There are several approaches to make a tree, but the easiest is to add properties with references to a parent and children of a document. This design pattern will be useful everywhere where there is a hierarchical data structure, eg., dependencies between employees in an organization.
Extended Reference Pattern
Last but not least, the design pattern can improve the performance of your application if you often make JOIN operations on your documents. To reduce them, you need to embed the most necessary properties of a document of the second collection into a document of the first one. In this way, you won't have to make JOIN operations so often, and if you need more properties of the document of the second collection, you always have access to take them. It can be useful also, eg., for e-commerce projects.
To sum up, these three patterns, I described shortly, can improve the organization of your data structure and optimize the performance of your application. I encourage you to read the MongoDB documentation to get to know more. If you want to explore more design patterns, I recommend you to join the data modeling course on MongoDB university.