Database Manual / Data Modeling

Data Consistency数据一致性

MongoDB gives you the flexibility to normalize or duplicate your data to optimize your application. If you duplicate data in your schema, you must decide how to keep duplicated data consistent across multiple collections. Some applications require duplicated data to be made consistent immediately, whereas other applications can tolerate reading stale data.MongoDB为您提供了规范化或复制数据的灵活性,以优化应用程序。如果在模式中重复数据,则必须决定如何在多个集合中保持重复数据的一致性。一些应用程序要求立即使重复数据保持一致,而其他应用程序可以容忍读取过时数据。

Use Cases用例

There are multiple ways to enforce data consistency in your application:有多种方法可以在应用程序中强制数据一致性:

Method方法Description描述Performance Impact性能影响Use Case用例
Transactions事务Updates to multiple collections occur in a single atomic operation.对多个集合的更新发生在单个原子操作中。Potentially high, due to read contention由于读争用,可能很高Your application must always return up-to-date data, and can tolerate potential negative performance impact during periods of heavy reads.应用程序必须始终返回最新数据,并且可以在大量读取期间容忍潜在的负面性能影响。
Embed related data嵌入相关数据Modify the application schema to embed related data in a single collection.修改应用程序架构,将相关数据嵌入到单个集合中。Low to moderate, depending on document size and indexes低到中等,取决于文档大小和索引Your application always reads and updates the related data at the same time. This solution simplifies your schema and prevents the need for $lookup operations.应用程序始终同时读取和更新相关数据。此解决方案简化了模式,并避免了$lookup操作的需要。
Atlas Database TriggersAtlas数据库触发器When an update occurs in one collection, triggers automatically update another collection.当一个集合中发生更新时,触发器会自动更新另一个集合。Low to moderate, with potential delays in processing triggered events低到中等,处理触发事件可能会延迟Your application can tolerate reading slightly stale data. Users can potentially see outdated data if they run a query immediately after an update, but before the trigger finishes updating the second collection.应用程序可以容忍读取稍微过时的数据。如果用户在更新后立即运行查询,但在触发器完成更新第二个集合之前,他们可能会看到过时的数据。

The best way to enforce data consistency depends on your application. To learn more about the benefits and implementation for each approach, refer to the corresponding documentation pages.强制数据一致性的最佳方法取决于应用程序。要了解每种方法的好处和实现的更多信息,请参阅相应的文档页面。

Tasks任务

To enforce data consistency in your application, see the following pages:要在应用程序中强制数据一致性,请参阅以下页面:

Details详情

The following factors can affect how you enforce data consistency.以下因素可能会影响您执行数据一致性的方式。

Data Staleness数据稳定性

Consider how important it is that your application returns the most up-to-date data. Some applications can return data that is minutes or hours stale with no impact to the user.考虑一下应用程序返回最新数据的重要性。某些应用程序可以返回几分钟或几小时的过时数据,而不会对用户产生影响。

For example, in an e-commerce application, a user needs to know immediately whether or not an item is available. This information is ideally kept as consistent as possible, even if it requires frequent updates.例如,在电子商务应用程序中,用户需要立即知道商品是否可用。即使需要频繁更新,这些信息也应尽可能保持一致。

In contrast, analytic queries are typically expected to read slightly stale data. It is not critical to keep analytic data completely consistent.相比之下,分析查询通常会读取稍微陈旧的数据。保持分析数据完全一致并不重要。

Your application's tolerance for stale data affects how to best keep data consistent. Frequently updating data in multiple collections reduces the risk that a user reads stale data. However, frequent updates can negatively impact your application's performance. When you enforce data consistency, balance user needs with performance impact.应用程序对过时数据的容忍度会影响如何最好地保持数据一致性。频繁更新多个集合中的数据可以降低用户读取过时数据的风险。然而,频繁的更新可能会对应用程序的性能产生负面影响。当您强制执行数据一致性时,请在用户需求和性能影响之间取得平衡。

Referential Integrity参照完整性

Referential integrity ensures that when an object is deleted, all references to that object are also deleted.引用完整性确保删除对象时,对该对象的所有引用也会被删除。

For example, an application has a products collection and a warehouse collection that contains references to the products collection. When a product is deleted from the products collection, the corresponding reference in the warehouse collection should also be deleted.例如,一个应用程序有一个products集合和一个warehouse集合,其中包含对products集合的引用。从products集合中删除产品时,warehouse集合中的相应参照也应删除。

If your schema requires referential integrity, incorporate logic into your application to keep references consistent. At minimum, your application logic should prevent errors when attempting to query a reference that does not exist.如果模式需要引用完整性,请将逻辑合并到应用程序中以保持引用的一致性。至少,应用程序逻辑应该防止在尝试查询不存在的引用时出错。

Learn More了解更多