Docs Menu

$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 fills null and missing values proportionally spanning the value range between surrounding non-null values. To determine the values for missing fields, $linearFill uses:

    • null이 아닌 주변 값의 차이입니다.

    • 주변 값 사이를 채울 null 필드의 수입니다.

  • $linearFill can fill multiple consecutive null values if those values are preceded and followed by non-null values 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 $linearFill to fill the null 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이 아닌 값이 앞뒤에 오지 않는 null 값은 null로 유지됩니다.

To fill missing field values using linear interpolation, you can use:

이 페이지의 예제에서는 시간 간격으로 단일 회사의 주가를 추적하는 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. $linearFill fills missing price values using linear interpolation based on the surrounding price 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
}
]

$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 the linearFillPrice field. $linearFill fills missing price values using linear interpolation based on the surrounding price values in the sequence.

    • locfPrice 을 채울 대상 필드 로 지정합니다.

      • { $locf: "$price" } is the value for the locfPrice field. locf stands for last observation carried forward. $locf fills missing price 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
}
]