Skip to content

Data Modelling

Estimated time to read: 2 minutes

MongoDB doesn't enforce how data is organised by default. This can lead to questions such as:

  • Where should Data be stored?
  • Where should sub-documents be created?
  • Where should arrays of values be used?
  • When should Data get its own collection?

What is Data Modelling

Data Modelling is a way of organising fields within a document to support application performance and querying capabilities.

The most important rule in Data Modelling is that Data is stored in the way that it is used. This notion determines the decision that is made about the shape of a document and the number of collections to be used within MongoDB.

Modelling Example

For example, an application is being built that stores patient information.

Each patient has a varied amount of information associated with them.

One patient might have multiple phone numbers, prescriptions, visit history, best means of contact, etc whilst another patient has no prescriptions or visit history associated with them.

From this basic example, it is easy to see how document sizes can vary drastically per patient.

Considerations

Following the example above, Main Considerations can be generated such as:

  • Information to Store
  • Contact Info
  • Visit History
  • Prescriptions
  • Age
  • Gender
  • et al

  • How this data will be queried

  • This is typically one of the most important main considerations
  • Who is using this application?
  • How is this application being used?

Users

Different data may be useful to different users depending on their usage of the application. For example, a Doctor may need to access the following:

  • Current Prescriptions
  • Diagnoses
  • Patient Contact Information
  • Ability to cross-reference medication
  • Side effects
  • Allergies

Optimising for fast data retrieval

Following the example above, it is useful to have two collections, one for the Patient and one of the Medication Reference:

  • Patient Data
  • Current Prescriptions
  • Diagnoses
  • Patient Contact Information
  • Medication Reference
  • Side effects
  • Allergies