정의
$linearFill버전 5.3에 추가 되었습니다.
Fills
nulland missing fields in a window using linear interpolation based on surrounding field values.$linearFillis only available in the$setWindowFieldsstage.
구문
The $linearFill expression has this syntax:
{ $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.
$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 } 전체 예시는 예시를 참조하세요 .
null앞뒤에null이 아닌 값이 오지 않는 값은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.
예시
이 페이지의 예제에서는 시간 간격으로 단일 회사의 주가를 추적하는 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 필드가 누락되었습니다.
선형 보간으로 누락된 값 채우기
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를 누락된 값을 채울 필드로 지정합니다.{ $linearFill: "$price" }as the value for the missing field.$linearFillfills missingpricevalues using linear interpolation based on the surroundingpricevalues 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 } ]
단일 단계에서 여러 채우기 메서드 사용
$setWindowFields 단계를 사용하여 누락된 값을 채우는 경우 채우는 필드와 다른 필드의 값을 설정할 수 있습니다. 따라서 단일 $setWindowFields 단계에서 여러 채우기 메서드를 사용하고 결과를 개별 필드에 출력할 수 있습니다.
다음 파이프라인은 선형 보간과 마지막 관측값 전달 방법을 사용하여 누락된 price 필드를 채웁니다.
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" }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.