$round

De $round operator wordt gebruikt om een getal af te ronden op een opgegeven decimaalteken. Het is handig in aggregaties waarbij numerieke precisie belangrijk is, zoals financiële berekeningen of statistische analyse.

Syntaxis

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

Parameterwaarden

Kenmerk Description
<number> Het getal dat moet worden afgerond.
<place> De decimale plaats waarop het getal moet worden afgerond.

Voorbeelden

Bekijk dit voorbeelddocument uit de winkelverzameling.

{
    "_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
                }
            ]
        }
    ]
}

Voorbeeld 1: de locatiecoördinaten van winkels afronden

Als u de breedtegraad en lengtegraad van alle winkels binnen het bedrijf First Up Consultants wilt afronden, voert u eerst een query uit om te filteren op de naam van het bedrijf. Gebruik vervolgens de operator $round op de velden lat en lon om het gewenste resultaat te retourneren.

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]
        }
    }
}])

De eerste drie resultaten die door deze query worden geretourneerd, zijn:

[
    {
        "_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
    }
]

Voorbeeld 2: afronden naar de dichtstbijzijnde duizend

Als u het totale verkoopvolume van winkels binnen het bedrijf First Up Consultants wilt afronden, voert u eerst een query uit om winkels te filteren op de bedrijfsnaam. Gebruik vervolgens de operator $round in het veld TotalSales om de waarde af te ronden op de dichtstbijzijnde duizend.

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

De eerste drie resultaten die door deze query worden geretourneerd, zijn:

[
    {
        "_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
    }
]