On this page本页内容
$linearFill
New in version 5.3.在版本5.3中新增。
Fills 使用基于周围字段值的线性插值填充窗口中的null
and missing fields in a window using linear interpolation based on surrounding field values.null
字段和缺失字段。
$linearFill
is only available in the 仅在$setWindowFields
stage.$setWindowFields
阶段可用。
The $linearFill
expression has this syntax:$linearFill
表达式具有以下语法:
{ $linearFill: <expression> }
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
$linearFill
fills 使用基于周围非null
and missing fields using linear interpolation based on surrounding non-null
field values. null
字段值的线性插值填充null
字段和缺少的字段。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. null
值之间的值范围内按比例填充null
值和缺失值。To determine the values for missing fields, 要确定缺失字段的值,$linearFill
uses:$linearFill
使用:
null
values.null
值的差异。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
值,则可以填充多个连续的null
值。
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 }
null
values that are not preceded and followed by non-null
values remain null
.null
值的null
值仍为null
。$fill
and $linearFill
$fill
和$linearFill
的比较To fill missing field values using linear interpolation, you can use:要使用线性插值填充缺少的字段值,可以使用:
The $fill
stage with { method: "linear" }
.$fill
阶段带有{ method: "linear" }
。
When you use the 使用$fill
stage, the field you specify in the output is the same field used as the source data. $fill
阶段时,输出中指定的字段与源数据使用的字段相同。See Fill Missing Field Values with Linear Interpolation.请参见使用线性插值填充缺少的字段值。
The $linearFill
operator inside of a $setWindowFields
stage.$setWindowFields
阶段中的$linearFill
运算符。
When you use the 使用$linearFill
operator, you can set values for a different field than the field used as the source data. $linearFill
运算符时,可以为不同于用作源数据的字段设置值。See Use Multiple Fill Methods in a Single Stage.请参见在单个阶段中使用多个填充方法。
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
字段。
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 }
time
field in ascending order, from earliest to latest.time
字段从早到晚的升序对文档进行排序。output
specifies:指定:
price
{ $linearFill: "$price" }
$linearFill
price
values using linear interpolation based on the surrounding price
values in the sequence.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 } ]
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 }
time
field in ascending order, from earliest to latest.time
字段从早到晚的升序对文档进行排序。output
specifies:指定:
linearFillPrice
as a target field to be filled.作为要填充的目标字段。
{ $linearFill: "$price" }
linearFillPrice
field. linearFillPrice
字段的值。$linearFill
price
values using linear interpolation based on the surrounding price
values in the sequence.price
值,使用线性插值填充缺少的price
值。locfPrice
as a target field to be filled.作为要填充的目标字段。
{ $locf: "$price" }
locfPrice
field. locf
$locf
price
values with the value from the previous document in the sequence.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 } ]
$linearFill
, you must use the sortBy field to sort your data.$linearFill
,必须使用sortBy
字段对数据进行排序。$linearFill
window function, $setWindowFields
returns an error if there are any repeated values in the sortBy field in a single partition.$linearFill
窗口函数时,如果单个分区中的sortBy
字段中有任何重复值,$setWindowFields
将返回错误。