$locf (aggregation)

On this page本页内容

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字段和缺少字段的值设置为字段的最后一个非空值。

$locf is only available in the $setWindowFields stage.仅在$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:本页上的示例使用一个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.集合中的某些文档缺少stock字段。

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. linearFillPrice字段的值。$linearFill fills missing 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" } 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
  }
]
←  $ln (aggregation)$log (aggregation) →