定義
$locfバージョン5.2の新機能。
Last observation carried forward(LOCF)法。ウィンドウ内の
nullおよび欠落しているフィールドの値として、フィールドの最後の null 以外の値を設定します。$locfis only available in the$setWindowFieldsstage.
構文
The $locf expression has this syntax:
{ $locf: <expression> }
式の詳細については、「式 」を参照してください。
動作
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 並べ替え順序 で null 以外の値の前に表示される欠落しているフィールド値は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
シーケンス内の最後の観察値に基づいて欠落しているフィールド値を入力するには、次を使用します。
{ method: "locf" }を持つ$fillステージ。$fillステージを使用する場合、出力で指定するフィールドはソースデータと同じフィールドになります。 「最後に確認された値に基づいて、欠落しているフィールド値を入力する 」を参照してください。The
$locfoperator inside of a$setWindowFieldsstage.When you use the
$locfoperator, 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フィールドが欠落しています。
欠落値を最後に確認された値で入力
The following example uses the $locf operator to set missing fields to the value from the last-observed non-null value:
db.stock.aggregate( [ { $setWindowFields: { sortBy: { time: 1 }, output: { price: { $locf: "$price" } } } } ] )
この例では、次のことが行われます。
sortBy: { time: 1 }は、各パーティション内のドキュメントをtimeで昇順(1)に並べ替えるので、最も古いtimeが最初になります。For documents where the
pricefield is missing, the$locfoperator sets thepriceto the last-observed value in the sequence.
出力例:
[ { _id: ObjectId("62169b65394d47411658b5f5"), time: ISODate("2021-03-08T09:00:00.000Z"), price: 500 }, { _id: ObjectId("62169b65394d47411658b5f6"), time: ISODate("2021-03-08T10:00:00.000Z"), price: 500 }, { _id: ObjectId("62169b65394d47411658b5f7"), time: ISODate("2021-03-08T11:00:00.000Z"), price: 515 }, { _id: ObjectId("62169b65394d47411658b5f8"), time: ISODate("2021-03-08T12:00:00.000Z"), price: 515 }, { _id: ObjectId("62169b65394d47411658b5f9"), time: ISODate("2021-03-08T13:00:00.000Z"), price: 515 }, { _id: ObjectId("62169b65394d47411658b5fa"), 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 thelinearFillPricefield.$linearFillfills missingpricevalues using linear interpolation based on the surroundingpricevalues in the sequence.
locfPrice入力するターゲット フィールドとして を使用します。{ $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.
出力例:
[ { _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 } ]