- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $locf (aggregation)
$locf (aggregation)¶
On this page
Definition¶
-
$locf
¶ New in version 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.$locf
is only available in the$setWindowFields
stage.
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
and missing field values that appear before non-null values
in the sort order remain 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.
Comparison of $fill
and $locf
¶
To fill missing field values based on the last observed value in a sequence, you can use:
The
$fill
stage with{ method: "locf" }
.When you use the
$fill
stage, 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.The
$locf
operator inside of a$setWindowFields
stage.When you use the
$locf
operator, 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.
Examples¶
The examples on this page use a stock
collection that contains
tracks a single company’s stock price at hourly intervals:
The price
field is missing for some of the documents in the
collection.
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:
In the example:
sortBy: { time: 1 }
sorts the documents in each partition bytime
in ascending order (1
), so the earliesttime
is first.- For documents where the
price
field is missing, the$locf
operator sets theprice
to the last-observed value in the sequence.
Example output:
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. As a result, you can use multiple fill methods in a single
$setWindowFields
stage and output the results in distinct
fields.
The following pipeline populates missing price
fields using
|linear-interpolation| and the last-observation-carried-forward method:
In the example:
sortBy: { time: 1 }
sorts the documents by thetime
field in ascending order, from earliest to latest.- output specifies:
linearFillPrice
as a target field to be filled.{ $linearFill: "$price" }
is the value for thelinearFillPrice
field.$linearFill
fills missingprice
values using |linear-interpolation| based on the surroundingprice
values in the sequence.
locfPrice
as a target field to be filled.{ $locf: "$price" }
is the value for thelocfPrice
field.locf
stands for last observation carried forward.$locf
fills missingprice
values with the value from the previous document in the sequence.
Example output: