$round

$round 연산자는 숫자를 지정된 소수 자릿수로 반올림하는 데 사용됩니다. 재무 계산이나 통계 분석 등 수치적 정밀도가 중요한 집계에 유용합니다.

문법

{
  $round: [ <number>, <place> ]
}

매개 변수

매개 변수 Description
<number> 반올림할 숫자.
<place> 숫자를 반올림할 10진 자리수.

예시

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

{
    "_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 - 매장의 위치 좌표 반올림

"First Up Consultants" 회사 내의 모든 매장의 위도와 경도를 반올림하려면, 먼저 회사 이름을 기준으로 필터링하기 위해 쿼리를 실행합니다. 그런 다음, 위도 및 lon 필드의 $round 연산자를 사용하여 원하는 결과를 반환합니다.

db.stores.aggregate([{
    $match: {
        company: {
            $in: ["First Up Consultants"]
        }
    }
}, {
    $project: {
        company: 1,
        "location.lat": 1,
        "location.lon": 1,
        roundedLat: {
            $round: ["$location.lat", 1]
        },
        roundedLon: {
            $round: ["$location.lon", 1]
        }
    }
}])

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

[
    {
        "_id": "39acb3aa-f350-41cb-9279-9e34c004415a",
        "location": {
            "lat": 87.2239,
            "lon": -129.0506
        },
        "company": "First Up Consultants",
        "roundedLat": 87.2,
        "roundedLon": -129.1
    },
    {
        "_id": "26afb024-53c7-4e94-988c-5eede72277d5",
        "location": {
            "lat": -29.1866,
            "lon": -112.7858
        },
        "company": "First Up Consultants",
        "roundedLat": -29.2,
        "roundedLon": -112.8
    },
    {
        "_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad",
        "location": {
            "lat": -0.2136,
            "lon": 108.7466
        },
        "company": "First Up Consultants",
        "roundedLat": -0.2,
        "roundedLon": 108.7
    }
]

예 2 - 가장 가까운 천 단위로 반올림

"First Up Consultants" 회사의 매장 총 판매량을 반올림하기 위해 먼저 회사 이름으로 매장을 필터링하는 쿼리를 실행하십시오. 그런 다음 totalSales 필드에서 $round 연산자를 사용하여 값을 가장 가까운 천으로 반올림합니다.

db.stores.aggregate([{
    $match: {
        company: {
            $in: ["First Up Consultants"]
        }
    }
}, {
    $project: {
        company: 1,
        "sales.totalSales": 1,
        roundedSales: {
            $round: ["$sales.totalSales", -3]
        }
    }
}])

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

[
    {
        "_id": "39acb3aa-f350-41cb-9279-9e34c004415a",
        "sales": {},
        "company": "First Up Consultants",
        "roundedSales": 279000
    },
    {
        "_id": "26afb024-53c7-4e94-988c-5eede72277d5",
        "sales": {},
        "company": "First Up Consultants",
        "roundedSales": 50000
    },
    {
        "_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad",
        "sales": {},
        "company": "First Up Consultants",
        "roundedSales": 69000
    }
]