- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $linearFill (aggregation)
$linearFill (aggregation)¶
On this page
Definition¶
-
$linearFill¶ New in version 5.3.
Fills
nulland missing fields in a window using :wikipedia:`linear interpolation <Linear_interpolation>` based on surrounding field values.$linearFillis only available in the$setWindowFieldsstage.
Syntax¶
The $linearFill expression has this syntax:
For more information on expressions, see Expressions.
Behavior¶
$linearFill fills null and missing fields using
|linear-interpolation| based on surrounding non-null field values.
The surrounding field values are determined by the sort order specified
in $setWindowFields.
$linearFillfillsnulland missing values proportionally spanning the value range between surrounding non-nullvalues. To determine the values for missing fields,$linearFilluses:- The difference of surrounding non-
nullvalues. - The number of
nullfields to fill between the surrounding values.
- The difference of surrounding non-
$linearFillcan fill multiple consecutivenullvalues if those values are preceded and followed by non-nullvalues according to the sort order specified in$setWindowFields.Example
If a collection contains these documents:
After using
$linearFillto fill thenullvalues, the documents become:For a complete example, see Examples.
nullvalues that are not preceded and followed by non-nullvalues remainnull.
Comparison of $fill and $linearFill¶
To fill missing field values using :wikipedia:`linear interpolation <Linear_interpolation>`, you can use:
The
$fillstage with{ method: "linear" }.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 with Linear Interpolation.The
$linearFilloperator inside of a$setWindowFieldsstage.When you use the
$linearFilloperator, 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 Linear Interpolation¶
To populate the missing price values using |linear-interpolation|,
use $linearFill inside of a $setWindowFields stage:
In the example:
sortBy: { time: 1 }sorts the documents by thetimefield in ascending order, from earliest to latest.- output specifies:
priceas the field for which to fill in missing values.{ $linearFill: "$price" }as the value for the missing field.$linearFillfills missingpricevalues using |linear-interpolation| based on the surroundingpricevalues 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 thetimefield in ascending order, from earliest to latest.- output specifies:
linearFillPriceas a target field to be filled.{ $linearFill: "$price" }is the value for thelinearFillPricefield.$linearFillfills missingpricevalues using |linear-interpolation| based on the surroundingpricevalues in the sequence.
locfPriceas a target field to be filled.{ $locf: "$price" }is the value for thelocfPricefield.locfstands for last observation carried forward.$locffills missingpricevalues with the value from the previous document in the sequence.
Example output:
Restrictions¶
- To use
$linearFill, you must use the sortBy field to sort your data. - When using
$linearFillwindow function,$setWindowFieldsreturns an error if there are any repeated values in the sortBy field in a single partition.