Database Manual / Data Modeling / Data Modeling Concepts

Embedded Data Versus References嵌入式数据与引用的对比

Effective data models support your application's needs. One key decision for your schema design is whether to embed data or use references.有效的数据模型支持应用程序的需求。模式设计的一个关键决定是是嵌入数据还是使用引用

Embedded Data Models嵌入式数据模型

You can embed related data in a single document. In the following example, the contact and access fields are embedded documents:您可以将相关数据嵌入到单个文档中。在以下示例中,contactaccess字段是嵌入式文档:

Data model with embedded fields that contain all related information.

Embedded data models are often denormalized, because frequently-accessed data is duplicated in multiple collections.嵌入式数据模型通常是非规范化的,因为频繁访问的数据在多个集合中重复。

Embedded data models let applications query related pieces of information in the same database record. As a result, applications require fewer queries and updates to complete common operations.嵌入式数据模型允许应用程序查询同一数据库记录中的相关信息。因此,应用程序需要更少的查询和更新来完成常见操作。

Use Cases用例

Use embedded data models in the following scenarios:在以下场景中使用嵌入式数据模型:

Embedding provides the following benefits:嵌入提供了以下好处:

  • Better performance for read operations读取操作性能更好
  • The ability to retrieve related data in a single database operation在单个数据库操作中检索相关数据的能力
  • The ability to to update related data in a single atomic write operation在单个原子写入操作中更新相关数据的能力

Query Embedded Data查询嵌入式数据

To query data within embedded documents, use dot notation. For examples of querying data in arrays and embedded documents, see:要查询嵌入式文档中的数据,请使用点表示法。有关查询数组和嵌入式文档中的数据的示例,请参阅:

Note

Document Size Limit文档大小限制

Documents in MongoDB must be smaller than 16 mebibytes.MongoDB中的文档必须小于16兆字节。

For large binary data, consider GridFS.对于大型二进制数据,请考虑GridFS

References参考文献

References store relationships between data by including links, called references, from one document to another. In the following example, the contact and access documents contain a reference to the user document.引用通过包含从一个文档到另一个文档的链接(称为引用)来存储数据之间的关系。在以下示例中,contactaccess文档包含对user文档的引用。

Data model using references to link documents. Both the ``contact`` document and the ``access`` document contain a reference to the ``user`` document.

References result in normalized data models because data is divided into multiple collections and not duplicated.引用会导致标准化的数据模型,因为数据被划分为多个集合,而不是重复的。

Use Cases用例

Use references to link related data in the following scenarios:在以下场景中使用引用链接相关数据:

  • Embedding would result in duplication of data but would not provide sufficient read performance advantages to outweigh the implications of the duplication. For example, when the embedded data frequently changes.嵌入会导致数据重复,但不会提供足够的读取性能优势来抵消重复的影响。例如,当嵌入的数据频繁更改时。
  • You need to represent complex many-to-many relationships or large hierarchical data sets.您需要表示复杂的多对多关系或大型层次数据集。
  • The related entity is frequently queried on its own. For example, if you have employee and department data, you may consider embedding department information in the employee documents. 相关实体经常被单独查询。例如,如果您有 employeedepartment数据,您可以考虑在employee文档中嵌入部门信息。However, if you often query for a list of departments, your application will perform best with a separate department collection that is linked to the employee collection with a reference.但是,如果您经常查询部门列表,则应用程序将最好使用单独的department集合,该集合通过引用链接到employee集合。

Query Normalized Data Models查询标准化数据模型

To query normalized data in multiple collections, MongoDB provides the following aggregation stages:为了查询多个集合中的规范化数据,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.有关各种树模型的示例,请参阅模型树结构