Database Manual / Data Modeling / Data Consistency

Enforce Data Consistency with Embedding通过嵌入强制数据一致性

If your schema stores the same data in multiple collections, you can embed related data to remove the duplication. The updated, denormalized schema keeps data consistent by maintaining data values in a single location.如果架构在多个集合中存储了相同的数据,则可以嵌入相关数据以删除重复。更新的非规范化模式通过在单个位置维护数据值来保持数据一致。

Embedding related data simplifies your schema and ensures that the user always reads the most current data. However, embedding may not be the best choice to represent complex relationships like many-to-many.嵌入相关数据简化了模式,并确保用户始终读取最新数据。然而,嵌入可能不是表示多对多复杂关系的最佳选择。

About this Task关于此任务

How to optimally embed related data depends on the queries run by your application. When you embed data in a single collection, consider the indexes that enable performant queries and structure your schema to allow for efficient, logical indexes.如何最佳地嵌入相关数据取决于应用程序运行的查询。当您在单个集合中嵌入数据时,请考虑启用高性能查询的索引,并构建模式以允许高效的逻辑索引。

To compare the benefits of embedding documents and references, see Embedded Data Versus References.要比较嵌入文档和引用的好处,请参阅嵌入数据与引用

Before you Begin开始之前

Review the different methods to enforce data consistency to ensure that embedding is the best approach for your application. For more information, see Data Consistency.审查强制数据一致性的不同方法,以确保嵌入是应用程序的最佳方法。有关详细信息,请参阅数据一致性

Updating how data is stored in your database can impact existing indexes and queries. When you update your schema, also update your application's indexes and queries to account for the schema changes.更新数据在数据库中的存储方式可能会影响现有的索引和查询。更新模式时,还应更新应用程序的索引和查询,以考虑模式更改。

The following example enforces data consistency in an e-commerce application. In the initial schema, product information is duplicated in the products and sellers collections. 以下示例强制电子商务应用程序中的数据一致性。在初始模式中,产品信息在productssellers集合中重复。The sellerId field in the products collection is a reference to the sellers collection, and links the data together.products集合中的sellerId字段是对卖家集合的引用,并将数据链接在一起。

// products collection

[
{
_id: 111,
sellerId: 456,
name: "sweater",
price: 30,
rating: 4.9,
color: "green"
},
{
_id: 222,
sellerId: 456,
name: "t-shirt",
price: 10,
rating: 4.2,
color: "blue"
},
{
_id: 333,
sellerId: 456,
name: "vest",
price: 20,
rating: 4.7,
color: "red"
}
]
// sellers collection

[
{
_id: 456,
name: "Cool Clothes Co",
location: {
address: "21643 Andreane Shores",
state: "Ohio",
country: "United States"
},
phone: "567-555-0105",
products: [
{
id: 111,
name: "sweater",
price: 30
},
{
id: 222,
name: "t-shirt",
price: 10
},
{
id: 333
name: "vest",
price: 20
}
]
}
]

Steps步骤

To denormalize the schema and enforce consistency, embed the product information inside of the sellers collection:为了使模式非规范化并强制一致性,将产品信息嵌入sellers集合中:

db.sellers.insertOne(
{
_id: 456,
name: "Cool Clothes Co",
location: {
address: "21643 Andreane Shores",
state: "Ohio",
country: "United States"
},
phone: "567-555-0105",
products: [
{
id: 111,
name: "sweater",
price: 30,
rating: 4.9,
color: "green"
},
{
id: 222,
name: "t-shirt",
price: 10,
rating: 4.2,
color: "blue"
},
{
id: 333,
name: "vest",
price: 20,
rating: 4.7,
color: "red"
}
]
}
)

Results结果

The updated schema returns all product information when a user queries for a particular seller. The updated schema does not require additional logic or maintenance to keep data consistent because data is denormalized in a single collection.当用户查询特定卖家时,更新的模式会返回所有产品信息。更新的模式不需要额外的逻辑或维护来保持数据一致,因为数据在单个集合中是非规范化的。

Next Steps后续步骤

After you restructure your schema, you can create indexes to support common queries. For example, if users often query for products by color, you can create an index on the products.color field:重构模式后,可以创建索引来支持常见查询。例如,如果用户经常按颜色查询产品,则可以在products.color字段上创建索引:

db.sellers.createIndex( { "products.color": 1 } )

Learn More了解更多