On this page本页内容
$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
阶段可用。
The $locf
expression has this syntax:$locf
表达式语法如下:
{ $locf: <expression> }
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
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
。
$fill
and $locf
$fill
和$locf
的比较To fill missing field values based on the last observed value in a sequence, you can use:要根据序列中最后观察到的值填充缺少的字段值,可以使用:
The $fill
stage with { method: "locf" }
.$fill
阶段带有{ method: "locf" }
。
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 Based on the Last Observed Value.请参见根据上次观察值填充缺少的字段值。
The $locf
operator inside of a $setWindowFields
stage.$setWindowFields
阶段中的$locf
运算符。
When you use the 使用$locf
operator, you can set values for a different field than the field used as the source data. $locf
运算符时,可以为不同于用作源数据的字段设置值。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.stock
字段。
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 }
time
in ascending order (1
), so the earliest time
is first.time
升序(1
)对每个分区中的文档进行排序,因此最早的time
是第一个。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 } ]
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
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" }
locfPrice
field. locfPrice
字段的值。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 } ]