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.文档结构的关键考虑因素是嵌入或使用引用的决定。
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:通常,在以下情况下使用嵌入式数据模型:
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.有关访问数组和嵌入文档中的数据的更多示例,请参见查询数组中的数据和查询嵌入文档中的数据。
Documents in MongoDB must be smaller than the MongoDB中的文档必须小于BSON文档大小的最大值。maximum BSON document size
.
Normalized data models describe relationships using references between documents.规范化数据模型使用文档之间的引用来描述关系。
In general, use normalized data models:通常,使用规范化数据模型:
To join collections, MongoDB provides the aggregation stages:为了加入集合,MongoDB提供了聚合阶段:
$lookup
$graphLookup
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.有关各种树模型的示例,请参见模型树结构。
For more information on data modeling with MongoDB, download the MongoDB Application Modernization Guide.有关MongoDB数据建模的更多信息,请下载MongoDB应用程序现代化指南。
The download includes the following resources:下载内容包括以下资源: