$linearFill

연산자는 $linearFill 문서 시퀀스에서 누락된 값을 보간합니다. $linearFill 연산자는 누락된 데이터에 대해 선형 보간을 수행하므로 시계열 데이터와 같은 값의 간격이 있는 데이터 세트에 유용합니다.

문법

{
    $linearFill: {
        input: < expression > ,
        sortBy: {
            < field >: < 1 or - 1 >
        }
    }
}

매개 변수

매개 변수 Description
input 누락된 값을 보간할 필드 또는 식입니다.
sortBy 데이터가 보간을 위해 정렬되는 필드를 정렬 순서와 함께 지정합니다(오름차순의 경우 1, 내림차순의 경우 -1).

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

예제 1: 누락된 판매 데이터 보간

누락된 판매 데이터를 보간하려면 쿼리를 실행하여 먼저 데이터 세트의 저장소를 이름으로 분할합니다. 그런 다음, $linearFill 연산자를 사용하여 파티션 내의 저장소에서 누락된 판매 데이터를 보간합니다.

db.stores.aggregate([{
        "$match": {
            "company": {
                "$in": ["First Up Consultants"]
            }
        }
    },
    {
        "$setWindowFields": {
            "partitionBy": "$name",
            "sortBy": {
                "storeOpeningDate": 1
            },
            "output": {
                "interpolatedSales": {
                    "$linearFill": "$sales.totalSales"
                }
            }
        }
    }
])

이 쿼리에서 반환된 처음 세 가지 결과는 다음과 같습니다.

[
    {
        "_id": "0f4c48fe-c43b-4083-a856-afe6dd902077",
        "name": "First Up Consultants | Appliance Bargains - Feilmouth",
        "interpolatedSales": 26630
    },
    {
        "_id": "c4883253-7ccd-4054-a7e0-8aeb202307b5",
        "name": "First Up Consultants | Appliance Bargains - New Kari",
        "interpolatedSales": 31568
    },
    {
        "_id": "a159ff5c-6ec5-4ca8-9672-e8903a54dd90",
        "name": "First Up Consultants | Appliance Bargains - Schadenstad",
        "interpolatedSales": 59926
    }
]