Database Manual / Reference / Query Language / Expressions

$linearFill (expression operator)(表达式运算符)

Definition定义

$linearFill

New in version 5.3.在版本5.3中新增。

Fills null and missing fields in a window using linear interpolation based on surrounding field values.使用基于周围字段值的线性插值填充window中的null字段和缺失字段。

$linearFill is only available in the $setWindowFields stage.$linearFill仅在$setWindowFields阶段可用。

Syntax语法

The $linearFill expression has this syntax:$linearFill表达式具有以下语法:

{ $linearFill: <expression> }

For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

$linearFill fills null and missing fields using linear interpolation based on surrounding non-null field values. $linearFill使用基于周围非空字段值的线性插值来填充空字段和缺失字段。The surrounding field values are determined by the sort order specified in $setWindowFields.周围的字段值由$setWindowFields中指定的排序顺序决定。

  • $linearFill fills null and missing values proportionally spanning the value range between surrounding non-null values. $linearFill按比例填充null值和缺失值,覆盖周围非null值之间的值范围。To determine the values for missing fields, $linearFill uses:要确定缺失字段的值,$linearFill使用:

    • The difference of surrounding non-null values.周围非null值的差异。
    • The number of null fields to fill between the surrounding values.要在周围值之间填充的null字段数。
  • $linearFill can fill multiple consecutive null values if those values are preceded and followed by non-null values according to the sort order specified in $setWindowFields.如果根据$setWindowFields中指定的排序顺序,这些值前后都有非null值,则$linearFill可以填充多个连续的null值。

    Example示例

    If a collection contains these documents:如果集合包含以下文档:

    { index: 0, value: 0 },
    { index: 1, value: null },
    { index: 2, value: null },
    { index: 3, value: null },
    { index: 4, value: 10 }

    After using $linearFill to fill the null values, the documents become:使用$linearFill填充null值后,文档变为:

    { index: 0, value: 0 },
    { index: 1, value: 2.5 },
    { index: 2, value: 5 },
    { index: 3, value: 7.5 },
    { index: 4, value: 10 }

    For a complete example, see Examples.有关完整示例,请参阅示例

  • null values that are not preceded and followed by non-null values remain null.前后没有非null值的null值仍为null

Comparison of $fill and $linearFill$fill$linearFill的比较

To fill missing field values using linear interpolation, you can use:要使用线性插值填充缺失的字段值,可以使用:

Examples示例

The examples on this page use a stock collection that contains tracks a single company's stock price at hourly intervals:此页面上的示例使用了一个stock集合,其中包含每小时跟踪一家公司股票价格的信息:

db.stock.insertMany( [
{
time: ISODate("2021-03-08T09:00:00.000Z"),
price: 500
},
{
time: ISODate("2021-03-08T10:00:00.000Z"),
},
{
time: ISODate("2021-03-08T11:00:00.000Z"),
price: 515
},
{
time: ISODate("2021-03-08T12:00:00.000Z")
},
{
time: ISODate("2021-03-08T13:00:00.000Z")
},
{
time: ISODate("2021-03-08T14:00:00.000Z"),
price: 485
}
] )

The price field is missing for some of the documents in the collection.集合中的某些文档缺少price字段。

Fill Missing Values with Linear Interpolation用线性插值填充缺失值

To populate the missing price values using linear interpolation, use $linearFill inside of a $setWindowFields stage:要使用线性插值填充缺失的price值,请在$setWindowFields阶段中使用$linearFill

db.stock.aggregate( [
{
$setWindowFields:
{
sortBy: { time: 1 },
output:
{
price: { $linearFill: "$price" }
}
}
}
] )

In the example:在示例中:

  • sortBy: { time: 1 } sorts the documents by the time field in ascending order, from earliest to latest.time字段从早到晚按升序对文档进行排序。
  • output specifies:指定:

    • price as the field for which to fill in missing values.作为填写缺失值的字段。
    • { $linearFill: "$price" } as the value for the missing field. 作为缺失字段的值。$linearFill fills missing price values using linear interpolation based on the surrounding price values in the sequence.$linearFill使用基于序列中周围price值的线性插值来填充缺失的price值。

Example output:示例输出:

[
{
_id: ObjectId("620ad555394d47411658b5ef"),
time: ISODate("2021-03-08T09:00:00.000Z"),
price: 500
},
{
_id: ObjectId("620ad555394d47411658b5f0"),
time: ISODate("2021-03-08T10:00:00.000Z"),
price: 507.5
},
{
_id: ObjectId("620ad555394d47411658b5f1"),
time: ISODate("2021-03-08T11:00:00.000Z"),
price: 515
},
{
_id: ObjectId("620ad555394d47411658b5f2"),
time: ISODate("2021-03-08T12:00:00.000Z"),
price: 505
},
{
_id: ObjectId("620ad555394d47411658b5f3"),
time: ISODate("2021-03-08T13:00:00.000Z"),
price: 495
},
{
_id: ObjectId("620ad555394d47411658b5f4"),
time: ISODate("2021-03-08T14:00:00.000Z"),
price: 485
}
]

Use Multiple Fill Methods in a Single Stage在单个阶段中使用多种填充方法

When you use the $setWindowFields stage to fill missing values, you can set values for a different field than the field you fill from. 当您使用$setWindowFields阶段填充缺失的值时,您可以为与填充字段不同的字段设置值。As a result, you can use multiple fill methods in a single $setWindowFields stage and output the results in distinct fields.因此,您可以在单个$setWindowFields阶段中使用多个填充方法,并在不同的字段中输出结果。

The following pipeline populates missing price fields using linear interpolation and the last-observation-carried-forward method:以下管道使用线性插值和最后一次观测结转方法填充缺失的price字段:

db.stock.aggregate( [
{
$setWindowFields:
{
sortBy: { time: 1 },
output:
{
linearFillPrice: { $linearFill: "$price" },
locfPrice: { $locf: "$price" }
}
}
}
] )

In the example:在示例中:

  • sortBy: { time: 1 } sorts the documents by the time field in ascending order, from earliest to latest.time字段从早到晚按升序对文档进行排序。
  • output specifies:指定:

    • linearFillPrice as a target field to be filled.作为要填充的目标字段。

      • { $linearFill: "$price" } is the value for the linearFillPrice field. linearFillPrice字段的值。$linearFill fills missing price values using linear interpolation based on the surrounding price values in the sequence.$linearFill使用基于序列中周围price值的线性插值来填充缺失的price值。
    • locfPrice as a target field to be filled.作为要填充的目标字段。

      • { $locf: "$price" } is the value for the locfPrice field. locf stands for last observation carried forward. $locf fills missing price values with the value from the previous document in the sequence.locfPrice字段的值。locf代表最后的观测结果。$locf用序列中前一个文档的值填充缺失的price值。

Example output:示例输出:

[
{
_id: ObjectId("620ad555394d47411658b5ef"),
time: ISODate("2021-03-08T09:00:00.000Z"),
price: 500,
linearFillPrice: 500,
locfPrice: 500
},
{
_id: ObjectId("620ad555394d47411658b5f0"),
time: ISODate("2021-03-08T10:00:00.000Z"),
linearFillPrice: 507.5,
locfPrice: 500
},
{
_id: ObjectId("620ad555394d47411658b5f1"),
time: ISODate("2021-03-08T11:00:00.000Z"),
price: 515,
linearFillPrice: 515,
locfPrice: 515
},
{
_id: ObjectId("620ad555394d47411658b5f2"),
time: ISODate("2021-03-08T12:00:00.000Z"),
linearFillPrice: 505,
locfPrice: 515
},
{
_id: ObjectId("620ad555394d47411658b5f3"),
time: ISODate("2021-03-08T13:00:00.000Z"),
linearFillPrice: 495,
locfPrice: 515
},
{
_id: ObjectId("620ad555394d47411658b5f4"),
time: ISODate("2021-03-08T14:00:00.000Z"),
price: 485,
linearFillPrice: 485,
locfPrice: 485
}
]

Restrictions限制

  • To use $linearFill, you must use the sortBy field to sort your data.要使用$linearFill,您必须使用sortBy字段对数据进行排序。
  • When using $linearFill window function, $setWindowFields returns an error if there are any repeated values in the sortBy field in a single partition.使用$linearFill窗口函数时,如果单个分区中的sortBy字段中有任何重复值,$setWindowFields将返回错误。