Skip to content
← All articlesSoftware & development

MongoDB Data Modelling: Documents and Relationships

Layered index-card stacks linked by fine thread to represent data relationships

Design a MongoDB model around the way an application reads and updates its data. Documents contain fields and values, collections group documents, and relationships can be represented through embedding or references. Flexibility is useful when it is accompanied by explicit application rules.

MongoDB modelling vocabulary

MongoDB modelling vocabulary
MongoDB modelling vocabulary
MongoDB modelling vocabulary
FocusPractical approachCheck before proceeding
DocumentA record containing fields and values.Keep related fields consistent with application requirements.
CollectionA group of documents used by the application.Apply validation where predictable data shapes matter.
RelationshipA connection represented by embedding or referencing.Choose using access patterns and update requirements.

Begin with access patterns

List the questions each important screen asks of the database. An order detail screen needs a different shape of information from a report across thousands of orders. Include updates and background processes in the exercise. Designing only around how an object looks in application code can miss the queries that dominate production use.

Compare embedding and references

Information frequently read together may suit an embedded structure, provided its size and update behaviour remain manageable. Separately maintained entities may suit references. For an order, distinguish the historical values recorded at purchase from current product information that may later change. That distinction should be explicit regardless of storage technique.

Define validation and change rules

Identify required fields, permitted types and relationships the application must maintain. Decide how older documents are read during a model change. A flexible schema does not mean that every shape is equally useful. Invalid or ambiguous data makes reporting, support and future migrations more difficult.

Test representative data

Use realistic document sizes and relationships in the prototype. Examine common queries, growth assumptions and update frequency. Record why the selected model fits the workload and what would indicate that it needs to change. Treat indexing and backup design as part of the production plan rather than a final adjustment.

Useful next steps

For a related perspective, read MongoDB Features: Documents, Indexes and Replication, Docker and Kubernetes: Packaging and Operating Applications, How Node.js Handles I/O and CPU-Heavy Work.

If you need help applying this to your business, explore our custom software development or discuss your project.

Sources and further reading

By Tech Me Today. Originally published September 22, 2023. Expanded on .

Explore all articles →
KEEP READING

More ideas. Fresh perspective.

Layered document cards with coloured fields arranged into database-like groups
Software & development

MongoDB Features: Documents, Indexes and Replication

MongoDB is a document database. Its document model, indexing and replication address different application needs; choosing it should begin with the records and queries your product actually requires. …

December 6, 2023↗
Rows of server cabinets in a modern data centre
Software & development

Docker and Kubernetes: Packaging and Operating Applications

Containers package applications with their dependencies to support more consistent delivery. Docker provides tools for working with containers, while Kubernetes orchestrates containerised workloads. T…

December 27, 2023↗
Paper illustration of a central hub connected to parallel processing paths
Software & development

How Node.js Handles I/O and CPU-Heavy Work

Node.js can coordinate many I/O operations without dedicating a JavaScript thread to each waiting request. That does not mean every operation runs in parallel or that CPU-heavy JavaScript becomes free…

November 29, 2023↗
LET’S MAKE IT HAPPEN

What would make your
business work better?

Tell us what you want to build, improve or automate.

Let’s talk about your project