$linearFill (aggregation)
定義
$linearFill
バージョン 5.3 で追加。
線形補間
を使用して、周囲のフィールド値に基づき、ウィンドウ 内の
null
と欠落フィールドを埋めます。$linearFill
は$setWindowFields
ステージでのみ使用可能です。
構文
$linearFill
式は次の構文をとります。
{ $linearFill: <expression> }
式の詳細については、「式演算子」を参照してください。
動作
$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.
$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
.例
If a collection contains these documents:
{ index: 0, value: 0 }, { index: 1, value: null }, { index: 2, value: null }, { index: 3, value: null }, { index: 4, value: 10 } After using
$linearFill
to fill thenull
values, the documents become:{ index: 0, value: 0 }, { index: 1, value: 2.5 }, { index: 2, value: 5 }, { index: 3, value: 7.5 }, { index: 4, value: 10 } For a complete example, see 例.
null
values that are not preceded and followed by non-null
values remainnull
.
と$fill
$linearFill
の比較
To fill missing field values using linear interpolation, you can use:
{ method: "linear" }
を持つ
$fill
ステージ。
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.$linearFill
$setWindowFields
ステージ内の 演算子。$linearFill
演算子を使用すると、ソースデータとして使用されるフィールドとは異なるフィールドに値を設定できます。 「 1 つのステージで複数の入力メソッドを使用する 」を参照してください。
例
このページの例では、1 つの会社の株価を時間単位で追跡する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 } ] )
コレクション内の一部のドキュメントでprice
フィールドが欠落しています。
Fill Missing Values with Linear Interpolation
To populate the missing price
values using linear interpolation,
use $linearFill
inside of a $setWindowFields
stage:
db.stock.aggregate( [ { $setWindowFields: { sortBy: { time: 1 }, output: { price: { $linearFill: "$price" } } } } ] )
この例では、次のことが行われます。
sortBy: { time: 1 }
は、ドキュメントをtime
フィールドで最初から最新の順に昇順でソートします。出力は以下を指定します。
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.
出力例:
[ { _id: ObjectId("620ad555394d47411658b5ef"), time: ISODate("2021-03-08T09:00:00.000Z"), price: 500 }, { _id: ObjectId("620ad555394d47411658b5f0"), time: ISODate("2021-03-08T10:00:00.000Z"), price: 507.5 }, { _id: ObjectId("620ad555394d47411658b5f1"), time: ISODate("2021-03-08T11:00:00.000Z"), price: 515 }, { _id: ObjectId("620ad555394d47411658b5f2"), time: ISODate("2021-03-08T12:00:00.000Z"), price: 505 }, { _id: ObjectId("620ad555394d47411658b5f3"), time: ISODate("2021-03-08T13:00:00.000Z"), price: 495 }, { _id: ObjectId("620ad555394d47411658b5f4"), time: ISODate("2021-03-08T14:00:00.000Z"), price: 485 } ]
1 つのステージで複数の入力メソッドを使用する
$setWindowFields
ステージを使用して欠落値を入力する場合、入力フィールドとは異なるフィールドに値を設定できます。 その結果、単一の$setWindowFields
ステージで複数の入力メソッドを使用して、結果を個別のフィールドに出力できます。
次のパイプラインは、price
線形補間 を使用して欠落している フィールドを入力します および last-observation-caled-forward メソッド。
db.stock.aggregate( [ { $setWindowFields: { sortBy: { time: 1 }, output: { linearFillPrice: { $linearFill: "$price" }, locfPrice: { $locf: "$price" } } } } ] )
この例では、次のことが行われます。
sortBy: { time: 1 }
は、ドキュメントをtime
フィールドで最初から最新の順に昇順でソートします。出力は以下を指定します。
入力するターゲット フィールドとして
linearFillPrice
を入力します。{ $linearFill: "$price" }
is the value for thelinearFillPrice
field.$linearFill
fills missingprice
values using linear interpolation based on the surroundingprice
values in the sequence.
入力するターゲット フィールドとして
locfPrice
を入力します。{ $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.
出力例:
[ { _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 } ]
制限事項
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.