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.