$round 演算子を使用すると、指定された小数位で数値を四捨五入できます。 これは、財務上の計算や統計分析など、数値の精度が重要な集計に役立ちます。
構文
{
$round: [ <number>, <place> ]
}
パラメーター
| パラメーター | Description |
|---|---|
<number> |
四捨五入する対象の数値。 |
<place> |
数値の四捨五入が行われる小数位。 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_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" 社内のすべての店舗の緯度と経度を四捨五入するには、まず、この会社の名前でフィルター処理するためのクエリを実行します。 次に、lat フィールドと 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]
}
}
}])
このクエリによって返される最初の 3 つの結果は次のとおりです。
[
{
"_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]
}
}
}])
このクエリによって返される最初の 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
}
]