定义
$linearFill5.3版本新增。
Fills
nulland missing fields in a window using linear interpolation based on surrounding field values.$linearFillis only available in the$setWindowFieldsstage.
语法
The $linearFill expression has this syntax:
{ $linearFill: <expression> }
有关表达式的更多信息,请参阅表达式。
行为
$linearFill fills null and missing fields using linear interpolation based on surrounding non-null field values. The surrounding field values are determined by the sort order specified in $setWindowFields.
$linearFillfillsnulland missing values proportionally spanning the value range between surrounding non-nullvalues. To determine the values for missing fields,$linearFilluses:周围非
null值之差。周围值之间要填充的
null字段的数量。
$linearFillcan fill multiple consecutivenullvalues if those values are preceded and followed by non-nullvalues according to the sort order specified in$setWindowFields.例子
如果集合中包含这些文档:
{ index: 0, value: 0 }, { index: 1, value: null }, { index: 2, value: null }, { index: 3, value: null }, { index: 4, value: 10 } After using
$linearFillto fill thenullvalues, the documents become:{ index: 0, value: 0 }, { index: 1, value: 2.5 }, { index: 2, value: 5 }, { index: 3, value: 7.5 }, { index: 4, value: 10 } 有关完整示例,请参阅示例。
null前后没有非null值的值仍为null。
Comparison of $fill and $linearFill
要使用线性插值填充缺失字段值,可以使用:
具有
{ method: "linear" }的$fill阶段。使用
$fill阶段时,您在输出中指定的字段与源数据使用的字段相同。 请参阅使用线性插值填充缺失字段值。The
$linearFilloperator inside of a$setWindowFieldsstage.When you use the
$linearFilloperator, you can set values for a different field than the field used as the source data. See Use Multiple Fill Methods in a Single Stage.
示例
本页上的示例使用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 } ] )
集合中的某些文档缺少 price 字段。
使用线性插值填充缺失值
To populate the missing price values using linear interpolation, use $linearFill inside of a $setWindowFields stage:
db.stock.aggregate( [ { $setWindowFields: { sortBy: { time: 1 }, output: { price: { $linearFill: "$price" } } } } ] )
在示例中:
sortBy: { time: 1 }按time字段以升序排列文档,从最早到最晚。输出指定:
price作为要填写缺失值的字段。{ $linearFill: "$price" }as the value for the missing field.$linearFillfills missingpricevalues using linear interpolation based on the surroundingpricevalues in the sequence.
示例输出:
[ { _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 } ]
在单个阶段使用多种填充方法
当您使用$setWindowFields阶段填充缺失值时,您可以为与填充字段不同的字段设置值。 因此,您可以在单个$setWindowFields阶段使用多种填充方法,并在不同字段中输出结果。
以下管道使用price 线性插值 填充缺失的 字段 以及最后观察结转方法:
db.stock.aggregate( [ { $setWindowFields: { sortBy: { time: 1 }, output: { linearFillPrice: { $linearFill: "$price" }, locfPrice: { $locf: "$price" } } } } ] )
在示例中:
sortBy: { time: 1 }按time字段以升序排列文档,从最早到最晚。输出指定:
linearFillPrice作为要填充的目标字段。{ $linearFill: "$price" }is the value for thelinearFillPricefield.$linearFillfills missingpricevalues using linear interpolation based on the surroundingpricevalues in the sequence.
locfPrice作为要填充的目标字段。
示例输出:
[ { _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 } ]
限制
To use
$linearFill, you must use the sortBy field to sort your data.When using
$linearFillwindow function,$setWindowFieldsreturns an error if there are any repeated values in the sortBy field in a single partition.