liveBook · Manning

Data model

As explained in MongoDB 8.0 in Action, Third Edition

A data model is a representation of the data structures, fields, and relationships that an application stores and accesses.

A data model describes how an application organizes the data it stores and retrieves: document fields, collections, and relationships. MongoDB lets documents have different fields and types, but that flexibility does not remove the need to plan. Design around how the application accesses data, then revisit the model as requirements change. section 5.0 section 5.6

Plan the model before deploying at production scale:

  • List the workload. For each important query, record the user action, whether it reads or writes, the fields involved, its frequency, and its priority. Include likely future operations. section 5.1 section 5.1.1
  • Map relationships among objects and decide which document shapes belong together. Keep similar documents in the same collection; mixing unrelated shapes complicates application logic and aggregation pipelines. section 5.1 section 2.6.1
  • Choose schema design patterns for the access patterns and costs you identified. Add indexes to support queries, and use collection-level schema validation where field types or values need rules. section 5.1 section 5.6

Modeling flight routes

An airline route application adds routes, searches by source and destination airports, updates route details, and checks route information. Its workload table makes the frequent reads visible before the route documents are designed. A route can keep a small, frequently accessed subset of airport information rather than a full airport document; this reduces retrieved document size but means airport changes may also require route updates. An airport document can instead keep a computed total_flights value to move repeated calculation from reads to writes. Choose between such patterns by weighing read performance, write work, duplication, and maintenance. section 5.1.1 section 5.1.3 section 5.3

Table 5.1 The workload necessary for an application

Action

Type

Information

Frequency

Priority

Add new route.

Write

flight_id, airline (id, name, alias, iata), src_airport, dst_airport, airplane, stops, codeshare

150 per day

High

Search flight routes.

Read

src_airport, dst_airport

22,000 per day

High

Update route details.

Write

flight_id, src_airport, dst_airport, airline (name, alias, iata)

100 per day

Medium

Check route information.

Read

src_airport, dst_airport, airplane, stops

40,000 per day

High

Use the route workload table to see how actions, fields, frequency, and priority inform modeling decisions. section 5.1.1

Store related data together when it fits the workload. Embedded documents and arrays can reduce the need for multidocument transactions, which generally cost more than single-document writes; transactions are not a substitute for schema design. Review and refactor collections as the application evolves. For event-driven applications, the same flexible document model can represent event objects in streams and stored data. section 8.4.3 section 2.6.1 section 15.3

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Works through workload assessment, relationship mapping, pattern selection, indexes, and validation; defers index details.
Examples
Uses an airline route workload table and route and airport documents to illustrate subset and computed patterns.
What it assumes
Assumes an application with identifiable read and write operations; explains the workload assessment used to begin schema design.
What you can do after
Build it from scratch by recording a workload, mapping relationships, selecting patterns, and indexing important queries.
How it connects
Connects schema design to query patterns, indexes, schema validation, embedding, and multidocument transactions.

Also explained in

Compare how 23 books explain data model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.