Data Model Design数据模型设计
On this page本页内容
Effective data models support your application needs. 有效的数据模型支持您的应用程序需求。The key consideration for the structure of your documents is the decision to embed or to use references.文档结构的关键考虑因素是决定嵌入或使用引用。
Embedded Data Models嵌入式数据模型
With MongoDB, you may embed related data in a single structure or document. 使用MongoDB,您可以将相关数据嵌入到单个结构或文档中。These schema are generally known as "denormalized" models, and take advantage of MongoDB's rich documents. 这些模式通常被称为“非规范化”模型,并利用了MongoDB丰富的文档。Consider the following diagram:请考虑以下图表:
Embedded data models allow applications to store related pieces of information in the same database record. 嵌入式数据模型允许应用程序将相关信息存储在同一数据库记录中。As a result, applications may need to issue fewer queries and updates to complete common operations.因此,应用程序可能需要发出更少的查询和更新来完成常见操作。
In general, use embedded data models when:通常,在以下情况下使用嵌入式数据模型:
you have "contains" relationships between entities.实体之间存在“包含”关系。See Model One-to-One Relationships with Embedded Documents.请参见与嵌入文档的模型一对一关系。you have one-to-many relationships between entities.实体之间存在一对多关系。In these relationships the "many" or child documents always appear with or are viewed in the context of the "one" or parent documents.在这些关系中,“多个”或子文档总是与“一个”或父文档一起出现,或者在其上下文中查看。See Model 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 single database operation. 通常,嵌入为读取操作提供了更好的性能,并能够在单个数据库操作中请求和检索相关数据。Embedded data models make it possible to update related 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 accessing data in arrays and embedded documents.有关访问数组和嵌入文档中数据的更多示例,请参阅查询数组中的数据和查询嵌入文档中的数据。
Embedded Data Model and Document Size Limit嵌入式数据模型和文档大小限制
Documents in MongoDB must be smaller than the maximum BSON document size.MongoDB中的文档必须小于最大BSON文档大小。
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 not provide sufficient read performance advantages to outweigh the implications 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:为了加入集合,MongoDB提供了聚合阶段:
$lookup
(Available starting in MongoDB 3.2)(从MongoDB 3.2开始提供)$graphLookup
(Available starting in MongoDB 3.4)(从MongoDB 3.4开始提供)
MongoDB also provides referencing to join data across collections.MongoDB还提供了跨集合联接数据的引用。
For an example of normalized data models, see Model One-to-Many Relationships with Document References.有关规范化数据模型的示例,请参阅具有文档引用的模型一对多关系。
For examples of various tree models, see Model Tree Structures.有关各种树模型的示例,请参阅模型树结构。
Further Reading进一步阅读
For more information on data modeling with MongoDB, download the MongoDB Application Modernization Guide有关使用MongoDB进行数据建模的更多信息,请下载MongoDB应用程序现代化指南.
。
The download includes the following resources:下载包括以下资源:
Presentation on the methodology of data modeling with MongoDB介绍使用MongoDB进行数据建模的方法White paper covering best practices and considerations for migrating to MongoDB from an RDBMS data model白皮书介绍了从RDBMS数据模型迁移到MongoDB的最佳实践和注意事项Reference MongoDB schema with its RDBMS equivalent参考MongoDB模式及其RDBMS等价物Application Modernization scorecard应用程序现代化记分卡