- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $linearFill (aggregation)
$linearFill (aggregation)¶
On this page
Definition¶
-
$linearFill
¶ New in version 5.3.
Fills
null
and missing fields in a window using :wikipedia:`linear interpolation <Linear_interpolation>` based on surrounding field values.$linearFill
is only available in the$setWindowFields
stage.
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
.
$linearFill
fillsnull
and missing values proportionally spanning the value range between surrounding non-null
values. To determine the values for missing fields,$linearFill
uses:- The difference of surrounding non-
null
values. - The number of
null
fields to fill between the surrounding values.
- The difference of surrounding non-
$linearFill
can fill multiple consecutivenull
values if those values are preceded and followed by non-null
values according to the sort order specified in$setWindowFields
.Example
If a collection contains these documents:
After using
$linearFill
to fill thenull
values, the documents become:For a complete example, see Examples.
null
values that are not preceded and followed by non-null
values remainnull
.
Comparison of $fill
and $linearFill
¶
To fill missing field values using :wikipedia:`linear interpolation <Linear_interpolation>`, you can use:
The
$fill
stage with{ method: "linear" }
.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 with Linear Interpolation.The
$linearFill
operator inside of a$setWindowFields
stage.When you use the
$linearFill
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 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 thetime
field in ascending order, from earliest to latest.- output specifies:
price
as the field for which to fill in missing values.{ $linearFill: "$price" }
as the value for the missing field.$linearFill
fills missingprice
values using |linear-interpolation| based on the surroundingprice
values 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:
Restrictions¶
- To use
$linearFill
, you must use the sortBy field to sort your data. - When using
$linearFill
window function,$setWindowFields
returns an error if there are any repeated values in the sortBy field in a single partition.