Data Model Design
Effective data models support your application needs. The keyconsideration for the structure of your documents is the decision toembed or to use references.
Embedded Data Models
With MongoDB, you may embed related data in a single structure ordocument. These schema are generally known as “denormalized” models,and take advantage of MongoDB’s rich documents. Consider the followingdiagram:
Embedded data models allow applications to store related pieces ofinformation in the same database record. As a result, applications mayneed to issue fewer queries and updates to complete common operations.
In general, use embedded data models when:
- you have “contains” relationships between entities. SeeModel One-to-One Relationships with Embedded Documents.
- you have one-to-many relationships between entities. In theserelationships the “many” or child documents always appear with orare viewed in the context of the “one” or parent documents. SeeModel One-to-Many Relationships with Embedded Documents.
In general, embedding provides better performance for read operations,as well as the ability to request and retrieve related data in a singledatabase operation. Embedded data models make it possible to updaterelated data in a single atomic write operation.
To access data within embedded documents, use dot notation to“reach into” the embedded documents. See query for data in arrays and query data in embedded documents for more examples on accessingdata in arrays and embedded documents.
Embedded Data Model and Document Size Limit
Documents in MongoDB must be smaller than the maximum BSONdocument size
.
For bulk binary data, consider GridFS.
Normalized Data Models
Normalized data models describe relationships using references between documents.
In general, use normalized data models:
- when embedding would result in duplication of data but would notprovide sufficient read performance advantages to outweigh theimplications of the duplication.
- to represent more complex many-to-many relationships.
- to model large hierarchical data sets.
To join collections, MongoDB provides the aggregation stages:
$lookup
(Available starting in MongoDB 3.2)$graphLookup
(Available starting in MongoDB 3.4)
MongoDB also provided referencing to join dataacross collections.
For an example of normalized data models, seeModel One-to-Many Relationships with Document References.
For examples of various tree models, seeModel Tree Structures.