Docs HomeMongoDB Manual

$locf (aggregation)

Definition定义

$locf

New in version 5.2. 5.2版新增。

Last observation carried forward. Sets values for null and missing fields in a window to the last non-null value for the field.最后一次观察继续进行。将窗口null和缺失字段的值设置为该字段的最后一个非null值。

$locf is only available in the $setWindowFields stage.$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的比较

To fill missing field values based on the last observed value in a sequence, 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:此页面上的示例使用了一个股票集合,其中包含按小时间隔跟踪单个公司股价的信息:

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 time in ascending order (1), so the earliest time is first.time升序(1)对每个分区中的文档进行排序,因此最早的time是第一个。
  • For documents where the price field is missing, the $locf operator sets the price to 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 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. $linearFill fills missing price values using linear interpolation based on the surrounding price values in the sequence.linearFillPrice字段的值。$linearFill使用基于序列中周围price值的线性插值来填充缺失的price值。
    • locfPrice as a target field to be filled.作为要填充的目标字段。

      • { $locf: "$price" } is the value for the locfPrice field. locfPrice字段的值。locf stands for last observation carried forward. 代表最后一次观测。$locf fills missing 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
}
]