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:contact和access字段是嵌入式文档:
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:在以下场景中使用嵌入式数据模型:
You have "contains" relationships between entities. For example, a实体之间存在“包含”关系。例如,包含contactsdocument that contains anaddress. See Model One-to-One Relationships with Embedded Documents.address的contacts文档。请参阅与嵌入式文档的模型一对一关系。You have one-to-many relationships between entities. In these relationships, the "many" or child documents are viewed in the context of the "one" or parent documents.实体之间存在一对多的关系。在这些关系中,“许多”或子文档在“一个”或父文档的上下文中查看。See Model One-to-Many Relationships with Embedded Documents.请参阅与嵌入式文档的模型一对多关系。
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:要查询嵌入式文档中的数据,请使用点表示法。有关查询数组和嵌入式文档中的数据的示例,请参阅:
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.contact和access文档包含对user文档的引用。
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相关实体经常被单独查询。例如,如果您有employeeanddepartmentdata, you may consider embedding department information in theemployeedocuments.employee和department数据,您可以考虑在employee文档中嵌入部门信息。However, if you often query for a list of departments, your application will perform best with a separate但是,如果您经常查询部门列表,则应用程序将最好使用单独的departmentcollection that is linked to theemployeecollection 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.有关各种树模型的示例,请参阅模型树结构。