MongoDB: Data Modeling Patterns

Where to start ?

Traditionally we tend to start ER or UML diagrams to capture the model once we have some amount of clarity on the requirements but the best way to start the data modelling is to write an application with in-memory and understand them based on some critical parameters like Usage Pattern, How the data is accessed, workload identification and Ratio between reads and writes.

Model for Simplicity or Performance

Simplicity means gather all the data in a flexible way and easy to understand by the developers. In other words, use of Embedded document approach to maintain most of the relationships over multiple collections in your schema.

Too many documents or too big document

When the document size grows then it can lead to a problem associated with a working set that exceeds RAM, which eventually access your disk to return the data set. The idea behind Subset Pattern is to split the collection into two collection based on Most used vs Less used part of documents.

Bucketing available products per day

Pattern to boost your read performance

The Computed Pattern is utilized when we have data that needs to be computed repeatedly in our application. For example, a customer search in the website we need to list only the cheapest products for the given property and date. This computation can be managed during the write channel and create the required document as shown below. In MongoDB, Aggregation Pipeline is a good framework to apply these kind of computations in a effective manner.

Computing Cheapest Product per Property

Pattern to Evolve your schema

Altering schema is a nightmare in Relational DB and often it requires a downtime in the application with well coordinated rollout plan. The Schema Versioning Pattern allows you to alter the schema without any downtime. It takes advantage of MongoDB’s support for differently shaped documents to exist in the same data base collection.

Conclusion

We have few more patterns like Attribute, Polymorphic, Tree etc., but i have highlighted only the patterns which i have experienced in our project. Its always important to pick the right pattern for your domain/use case to get a successful outcome. Also you will get a greater results when combining the patterns to model your schema.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rabbani Kaja Mohideen

Rabbani Kaja Mohideen

Passionate engineer and Enterprise Architecture Practitioner who loves to learn & grow.