
MongoDB: Data Modeling for Real Projects
MongoDB data modeling for real projects: embedding vs referencing, common patterns, the right indexes, schema validation, and the mistakes that kill performance.
Quick answer
In MongoDB, design the data model around the application's real queries, not relational tables. Embed data that is always read with its parent and has bounded growth; reference data that is read independently, grows a lot, or is shared by many documents. Build the right index for every frequent query (usually a compound index ordered by equality, sort, then range), avoid unbounded arrays, and block invalid data with $jsonSchema validation.
Start the design from your queries
With a relational database we usually normalize tables first and write queries later. MongoDB is the reverse: list the screens and reports your application needs, then shape the model so each one is answered with as few queries and as little extra processing as possible.
Embed or reference?
The simple rule: what is read together is stored together, as long as its growth is bounded. Each MongoDB document can be at most 16 MB.
- Embed: bounded one-to-few relations, such as a customer's addresses or an order's line items
- Embed: data that is almost always displayed with its parent
- Reference: one-to-very-many relations, such as reviews of a popular product
- Reference: independent data shared by several documents, such as an author or a category
Common modeling patterns
A few well-known patterns that come up again and again in real projects:
- Subset: embed only the most recent items, such as the last ten reviews, and keep the rest separate
- Extended reference: copy a few frequently used fields of the referenced document, such as a customer's name, to reduce joins
- Computed: calculate values such as counts or totals on write instead of on every read
- Bucket and time series: group time-based data or use a time series collection
Indexing correctly
Without the right index, MongoDB scans the whole collection and everything slows down as data grows. For compound indexes apply the ESR rule: equality fields first, then the sort field, then range fields.
Use explain to check that a query really uses the index. Extra indexes are not free and slow down every write, so remove unused ones. For data that must expire after a while, a TTL index is useful.
Validation and data integrity
MongoDB's flexibility does not mean no rules. With collection-level $jsonSchema validation, define required fields and types so an application bug cannot insert broken data.
MongoDB supports multi-document transactions, but the best design usually keeps important operations atomic on a single document and reserves transactions for cases that truly need them.
Common mistakes that kill performance
These mistakes show up often in young projects:
- Arrays that grow without limit, such as every order a customer ever placed inside the customer document
- Using $lookup on every page instead of modeling for the query
- Missing indexes for frequent queries and sorts
- Storing large files inside documents instead of in file storage
Frequently asked questions
Does MongoDB support transactions?
Yes. MongoDB supports multi-document ACID transactions, but they have a performance cost; it is better to design the model so most operations are atomic on a single document.
When is MongoDB not the right choice?
When data is highly relational and complex joins and precise financial reporting are central, a relational database such as PostgreSQL is usually the simpler and safer choice.
How do we find slow queries?
Use explain to view a query's execution plan, enable the database profiler to log slow queries, and review index suggestion tools. A missing index is the most common cause of slowness.
