Skip to content

Chapter 7 of 8

Schema Design Patterns

MongoDB's flexible document model enables several schema design patterns that differ from traditional relational approaches. The two fundamental strategies are embedding and referencing. Embedded documents store related data within a single document, which is denormalized and ideal for one-to-few relationships where data is always accessed together. Referenced documents, by contrast, store relationships using ObjectId references in a normalized manner, which works better for one-to-many or many-to-many relationships and for data that is accessed independently. Embedding provides faster reads by avoiding joins, while referencing offers smaller document sizes and easier updates of shared data.

Several guidelines help decide when to use each approach. Embedding is preferable when data is always accessed together, when the relationship is one-to-few, and when data does not change frequently. Referencing is preferable when data is accessed independently, when relationships are one-to-many or many-to-many, when documents might otherwise exceed MongoDB's 16 megabyte BSON document size limit, and when data is updated frequently. For files larger than 16MB, GridFS provides a specification for splitting files into 255KB chunks stored across two collections: fs.files for metadata and fs.chunks for binary data.

Beyond embedding and referencing, several advanced patterns address specialized needs. Capped collections are fixed-size collections that automatically overwrite the oldest documents when full, maintaining insertion order and supporting high-throughput operations; they are ideal for logs and caches. The bucket pattern groups related data, such as time-series measurements, into fixed-size buckets within a single document, reducing document count and improving read performance. The polymorphic pattern stores documents of different shapes in the same collection, distinguished by a discriminator field like type, leveraging MongoDB's flexible schema for varied entity types. Finally, the attribute pattern restructures documents with many similar fields into an array of key-value pairs, enabling efficient indexing and querying across any attribute.

All chapters
  1. 1Foundations of MongoDB
  2. 2CRUD Operations and Querying
  3. 3The Aggregation Framework
  4. 4Indexing and Query Performance
  5. 5Replication for High Availability
  6. 6Sharding for Horizontal Scaling
  7. 7Schema Design Patterns
  8. 8Advanced Features and Ecosystem

Drill it

Reading is not remembering. These come from the Nosql MongoDB deck:

Q

What is MongoDB?

MongoDB is a NoSQL document-oriented database that stores data in flexible, JSON-like documents called BSON. It is designed for scalability and high performance...

Q

What is the document model in MongoDB?

The document model stores data as documents, which are rich data structures similar to JSON objects. Each document can contain nested fields, arrays, and sub-do...

Q

What is BSON?

BSON (Binary JSON) is the binary-encoded serialization format MongoDB uses to store documents and make remote procedure calls. It extends JSON with additional d...

Q

What is an ObjectId in MongoDB?

An ObjectId is a 12-byte unique identifier automatically generated by MongoDB as the default _id field for each document. It consists of: A 4-byte timestampA 5-...