定義
$linearFillバージョン5.3の新機能。
線形補間
nullを使用して、周囲のフィールド値に基づき、 ウィンドウ 内の と欠落フィールドを埋めます。$linearFillis only available in the$setWindowFieldsstage.
構文
The $linearFill expression has this syntax:
{ $linearFill: <expression> }
式の詳細については、「式 」を参照してください。
動作
$linearFillnullnullは、周囲の 以外のフィールド値に基づき、線形補間を使用して と欠落フィールドを埋めます。周囲のフィールド値は、$setWindowFields で指定された並べ替え順序によって決定されます。
$linearFillfillsnulland missing values proportionally spanning the value range between surrounding non-nullvalues. To determine the values for missing fields,$linearFilluses:周囲の
nullの値との差。周囲の値の間に入力する
nullフィールドの数。
$linearFillcan fill multiple consecutivenullvalues if those values are preceded and followed by non-nullvalues according to the sort order specified in$setWindowFields.例
これらのドキュメントがコレクションに含まれている場合:
{ index: 0, value: 0 }, { index: 1, value: null }, { index: 2, value: null }, { index: 3, value: null }, { index: 4, value: 10 } After using
$linearFillto fill thenullvalues, 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 } 完全な例については、「例 」を参照してください。
nullnullの値が先行せず、その後に続く値はnullのままになります。
Comparison of $fill and $linearFill
線形補間 を使用して欠落しているフィールド値を埋め込むには、以下を使用します。
{ method: "linear" }を持つ$fillステージ。$fillステージを使用する場合、出力で指定するフィールドはソースデータと同じフィールドになります。 「欠落しているフィールド値を線形補間で入力する 」を参照してください。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.
例
このページの例では、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フィールドが欠落しています。
線形補間で欠損値を埋め込む
線形補間を使用して欠落しているprice 値を入力するには、 ステージ内で $linearFill$setWindowFieldsを使用します。
db.stock.aggregate( [ { $setWindowFields: { sortBy: { time: 1 }, output: { price: { $linearFill: "$price" } } } } ] )
この例では、次のことが行われます。
sortBy: { time: 1 }は、ドキュメントをtimeフィールドで最初から最新の順に昇順でソートします。出力は以下を指定します。
price欠落値を入力するフィールドとして。{ $linearFill: "$price" }欠落しているフィールドの値として 。$linearFillpriceは、シーケンス内の周囲のprice値に基づき、線形補間を使用して欠落している 値を埋めます。
出力例:
[ { _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ステージで複数の入力メソッドを使用して、結果を個別のフィールドに出力できます。
次のパイプラインは、線形補間 と last-observation-carried-forward メソッドを使用して、欠落している price フィールドを入力します。
db.stock.aggregate( [ { $setWindowFields: { sortBy: { time: 1 }, output: { linearFillPrice: { $linearFill: "$price" }, locfPrice: { $locf: "$price" } } } } ] )
この例では、次のことが行われます。
sortBy: { time: 1 }は、ドキュメントをtimeフィールドで最初から最新の順に昇順でソートします。出力は以下を指定します。
linearFillPrice入力するターゲット フィールドとして を使用します。{ $linearFill: "$price" }は、linearFillPriceフィールドの値です。$linearFillpriceは、シーケンス内の周囲のprice値に基づき、線形補間を使用して欠落している 値を埋めます。
locfPrice入力するターゲット フィールドとして を使用します。{ $locf: "$price" }は、locfPriceフィールドの値です。locfは、転送された最後の観察を示します。$locfは、欠落しているprice値をシーケンス内の前のドキュメントの値で埋めます。
出力例:
[ { _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
$linearFillwindow function,$setWindowFieldsreturns an error if there are any repeated values in the sortBy field in a single partition.