$locf (aggregation)
On this page本页内容
Definition定义
$locfNew in version 5.2.5.2版新增。Last observation carried forward. Sets values for最后一次观察继续进行。将窗口中nulland missing fields in a window to the last non-null value for the field.null和缺失字段的值设置为该字段的最后一个非null值。$locfis only available in the$setWindowFieldsstage.$locf仅在$setWindowFields阶段中可用。
Syntax语法
The $locf expression has this syntax:$locf表达式的语法如下:
{ $locf: <expression> }
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
Behavior行为
If a field being filled contains both 如果要填充的字段同时包含null and non-null values, $locf sets the null and missing values to the field's last known non-null value according to the sort order specified in $setWindowFields.null值和非null值,$locf会根据$setWindowFields中指定的排序顺序,将null值和缺失值设置为字段最后一个已知的非null值。
排序顺序中出现在非null and missing field values that appear before non-null values in the sort order remain null.null值之前的null和缺失字段值保持为null。
If a field being filled contains only 如果要填充的字段在分区中只包含null or missing values in a partition, $locf sets the field value to null for that partition.null或缺少值,$locf会将该分区的字段值设置为null。
Comparison of $fill and $locf$fill和$locf的比较
$fill and $locfTo fill missing field values based on the last observed value in a sequence, you can use:要根据序列中最后一个观察到的值填充缺失的字段值,可以使用:
The带有$fillstage with{ method: "locf" }.{ method: "locf" }的$fill阶段。When you use the使用$fillstage, the field you specify in the output is the same field used as the source data. See Fill Missing Field Values Based on the Last Observed Value.$fill阶段时,在输出中指定的字段与源数据使用的字段相同。请参见根据上次观测值填充缺少的字段值。The$locfoperator inside of a$setWindowFieldsstage.$setWindowFields阶段内的$locf运算符。When you use the使用$locfoperator, 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.$locf运算符时,可以为不同于用作源数据的字段的字段设置值。请参见在单个阶段中使用多种填充方法。
Examples实例
The examples on this page use a 此页面上的示例使用了一个股票集合,其中包含按小时间隔跟踪单个公司股价的信息:stock collection that contains tracks a single company's stock price at hourly intervals:
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 the Last Observed Value用上次观测值填充缺失值
The following example uses the 以下示例使用$locf operator to set missing fields to the value from the last-observed non-null value:$locf运算符将缺少的字段设置为上次观察到的非null值中的值:
db.stock.aggregate( [
{
$setWindowFields: {
sortBy: { time: 1 },
output: {
price: { $locf: "$price" }
}
}
}
] )
In the example:在示例中:
sortBy: { time: 1 }sorts the documents in each partition by按timein ascending order (1), so the earliesttimeis first.time升序(1)对每个分区中的文档进行排序,因此最早的time是第一个。For documents where the对于缺少pricefield is missing, the$locfoperator sets thepriceto the last-observed value in the sequence.price字段的文档,$locf运算符将price设置为序列中最后一个观察到的值。
Example output:示例输出:
[
{
_id: ObjectId("62169b65394d47411658b5f5"),
time: ISODate("2021-03-08T09:00:00.000Z"),
price: 500
},
{
_id: ObjectId("62169b65394d47411658b5f6"),
time: ISODate("2021-03-08T10:00:00.000Z"),
price: 500
},
{
_id: ObjectId("62169b65394d47411658b5f7"),
time: ISODate("2021-03-08T11:00:00.000Z"),
price: 515
},
{
_id: ObjectId("62169b65394d47411658b5f8"),
time: ISODate("2021-03-08T12:00:00.000Z"),
price: 515
},
{
_id: ObjectId("62169b65394d47411658b5f9"),
time: ISODate("2021-03-08T13:00:00.000Z"),
price: 515
},
{
_id: ObjectId("62169b65394d47411658b5fa"),
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按照timefield in ascending order, from earliest to latest.time字段从最早到最晚的升序对文档进行排序。outputspecifies:指定:linearFillPriceas a target field to be filled.作为要填充的目标字段。{ $linearFill: "$price" }is the value for the是linearFillPricefield.$linearFillfills missingpricevalues using linear interpolationbased on the surrounding
pricevalues in the sequence.linearFillPrice字段的值。$linearFill使用基于序列中周围price值的线性插值来填充缺失的
price值。
locfPriceas a target field to be filled.作为要填充的目标字段。{ $locf: "$price" }is the value for the是locfPricefield.locfPrice字段的值。locfstands for last observation carried forward.代表最后一次观测。$locffills missing用序列中上一个文档的值填充缺少的pricevalues 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
}
]