Database Manual / Data Modeling / Schema Design Patterns / Computed Values

Use the Approximation Pattern使用近似模式

Use the approximation pattern when you have values that change frequently, but users don't need to know precise values. Instead of updating values every time data changes, the approximation pattern updates data based on a larger granularity, which results in fewer updates and a lower application workload.当值经常变化,但用户不需要知道精确的值时,使用近似模式。近似模式不是每次数据更改时都更新值,而是基于更大的粒度更新数据,这导致更新更少,应用程序工作负载更低。

About this Task关于此任务

The approximation pattern is useful when values don't need to be reported exactly. For example:当不需要精确报告值时,近似模式很有用。例如:

  • City population城市人口
  • Website visits网站访问量
  • Airline travelers航空公司旅客

The preceding measurements are typically useful when approximated. The application can save time and resources by updating stored values by hundreds or thousands, depending on the scale of the data.上述测量值在近似时通常很有用。根据数据的规模,应用程序可以通过更新数百或数千个存储值来节省时间和资源。

Steps步骤

In this example, an application displays population data for a city of roughly 40,000 people. Application users are primarily looking for overall trends and don't need to know the exact city population.在这个例子中,一个应用程序显示了一个大约有40000人的城市的人口数据。应用程序用户主要是在寻找整体趋势,不需要知道确切的城市人口。

1

Insert sample data.插入示例数据。

db.population.insertOne( {
city: "New Perth",
population: 40000,
date: ISODate("2022-09-15")
} )
2

Implement the approximation pattern.实施近似模式。

The actual population value changes many times in a single day. Rather than updating the population value with each change, use application logic to insert a new document every time the population changes by 100.实际人口值在一天内会发生多次变化。每次人口变化100时,使用应用程序逻辑插入一个新文档,而不是每次更新人口值。

For example, your application logic might resemble the following:例如,应用程序逻辑可能类似于以下内容:

let population = 40000

function updateStoredPopulation(curr_population, new_population) {

let population_change = Math.abs(curr_population - new_population)

if (population_change >= 100) {
db.population.insertOne(
{
city: "New Perth",
population: new_population,
date: Date()
}
)
population = new_population
}
}

Note

The preceding example is only illustrative and does not use accurate syntax. To learn the correct syntax for your application, refer to your corresponding driver documentation.前面的例子只是说明性的,没有使用准确的语法。要了解应用程序的正确语法,请参阅相应的驱动程序文档

Results结果

The preceding application logic might result in these documents:前面的应用程序逻辑可能会产生以下文档:

db.population.insertMany( [
{
city: "New Perth",
population: 40100,
date: ISODate("2024-09-20")
},
{
city: "New Perth",
population: 40200,
date: ISODate("2024-10-01")
},
{
city: "New Perth",
population: 40300,
date: ISODate("2024-10-09")
},
] )

Note

How the updated values are gathered depends on your scenario. In this example, updated population values can be gathered from census reports.如何集合更新值取决于场景。在这个例子中,可以从人口普查报告中集合更新的人口值。

By updating the population with a granularity of 100, the approximation pattern reduces the number of updates to 1% of the updates that would be required to track individual population changes.通过以100的粒度更新总体,近似模式将更新次数减少到跟踪单个总体变化所需更新的1%。

Users can see the population increasing over time, which meets their needs of seeing high-level trends.用户可以看到人口随着时间的推移而增长,这满足了他们看到高层趋势的需求。

Learn More了解更多