$locf

연산자는 $locf 창이 있는 쿼리의 파티션 내에서 마지막으로 관찰된 null이 아닌 값을 전달합니다. $locf 연산자는 시계열 데이터 또는 다른 데이터 세트의 누락된 데이터 요소를 간격으로 채우는 데 특히 유용합니다.

문법

{
  $locf: {
    input: <expression>,
    sortBy: <document>
  }
}

매개 변수

매개 변수 Description
input 값을 전파하려는 필드로 확인되는 식입니다.
sortBy 파티션의 정렬 순서를 지정하는 문서입니다.

예시

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

{
    "_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: 누락된 시계열 데이터를 채우는 데 사용 $locf

"First Up Consultants" 회사 내의 저장소에서 lastUpdated 필드에 대한 가장 최근의 null이 아닌 값을 전파하려면 먼저 쿼리를 실행하여 회사 내에서 매장을 분할합니다. 그런 다음 $lecf 연산자를 사용하여 파티션 내의 모든 저장소에 대한 필드에 대한 마지막 null이 아닌 값을 전파합니다.

db.stores.aggregate([{
    "$match": {
        "company": {
            "$in": ["First Up Consultants"]
        }
    }
}, {
    "$setWindowFields": {
        "partitionBy": "$name",
        "sortBy": {
            "sales.revenue": 1
        },
        "output": {
            "lastUpdatedDate": {
                "$locf": {
                    "lastUpdated": 1
                }
            }
        }
    }
}, {
    "$project": {
        "company": 1,
        "name": 1,
        "lastObservedDiscount": 1
    }
}])

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

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