Docs Menu

$round (aggregation)

項目一覧

$round

$round rounds a number to a whole integer or to a specified decimal place.

$roundの構文は次のとおりです。

{ $round : [ <number>, <place> ] }
フィールド
タイプ
説明

<number>

数値

Can be any valid that resolves to a number. Specifically, the expression must resolve to an integer, double, decimal, or long.

$roundは、式が数値以外のデータ型に解決された場合はエラーを返します。

<place>

integer

任意 Can be any valid that resolves to an integer between -20 and 100, exclusive. e.g. -20 < place < 100. Defaults to 0 if unspecified.

  • If <place> resolves to a positive integer, $round rounds to <place> decimal places.

    たとえば、 $round : [1234.5678, 2] rounds to two decimal places and returns 1234.57.

  • If <place> resolves to a negative integer, $round rounds using the digit <place> to the left of the decimal.

    たとえば、 $round : [1234.5678, -2] uses the 2nd digit to the left of the decimal (3) and returns 1200.

    If the absolute value of <place> equals or exceeds the number of digits to the left of the decimal, $round returns 0.

    たとえば、 $round : [ 1234.5678, -4] specifies the fourth digit to the left of the decimal. This equals the number of digits left of the decimal and returns 0.

  • If <place> resolves to 0, $round rounds using the first digit to the right of the decimal and returns rounded integer value.

    たとえば、 $round : [1234.5678, 0]1235.

When rounding on a value of 5, $round rounds to the nearest even value. For example, consider the following sample documents:

{_id : 1, "value" : 10.5},
{_id : 2, "value" : 11.5},
{_id : 3, "value" : 12.5},
{_id : 4, "value" : 13.5}

$round : [ "$value", 0] returns the following:

{_id : 1, "value" : 10},
{_id : 2, "value" : 12},
{_id : 3, "value" : 12},
{_id : 4, "value" : 14}

The value 10.5 is closest to the even value 10, while the values 11.5 and 12.5 are closest to the even value 12. Rounding to the nearest even value supports more even distribution of rounded data than always rounding up or down.

The returned data type matches the data type of the input expression or value.

  • If the first argument resolves to a value of null or refers to a field that is missing, $round returns null.

  • If the first argument resolves to NaN, $round returns NaN.

  • If the first argument resolves to negative or positive infinity, $round returns negative or positive infinity respectively.

結果

{ $round: [ NaN, 1] }

NaN

{ $round: [ null, 1] }

null

{ $round : [ Infinity, 1 ] }

Infinity

{ $round : [ -Infinity, 1 ] }

-Infinity

次のドキュメントを含むsamplesという名前のコレクションを作成します。

db.samples.insertMany(
[
{ _id: 1, value: 19.25 },
{ _id: 2, value: 28.73 },
{ _id: 3, value: 34.32 },
{ _id: 4, value: -45.39 }
]
)
  • The following aggregation returns value rounded to the first decimal place:

    db.samples.aggregate([
    { $project: { roundedValue: { $round: [ "$value", 1 ] } } }
    ])

    この操作は次の結果を返します。

    { "_id" : 1, "roundedValue" : 19.2 }
    { "_id" : 2, "roundedValue" : 28.7 }
    { "_id" : 3, "roundedValue" : 34.3 }
    { "_id" : 4, "roundedValue" : -45.4 }
  • The following aggregation returns value rounded using the first digit to the left of the decimal:

    db.samples.aggregate([
    { $project: { roundedValue: { $round: [ "$value", -1 ] } } }
    ])

    この操作は次の結果を返します。

    { "_id" : 1, "roundedValue" : 10 }
    { "_id" : 2, "roundedValue" : 20 }
    { "_id" : 3, "roundedValue" : 30 }
    { "_id" : 4, "roundedValue" : -50 }
  • The following aggregation returns value rounded to the whole integer:

    db.samples.aggregate([
    { $project: { roundedValue: { $round: [ "$value", 0 ] } } }
    ])

    この操作は次の結果を返します。

    { "_id" : 1, "roundedValue" : 19 }
    { "_id" : 2, "roundedValue" : 29 }
    { "_id" : 3, "roundedValue" : 34 }
    { "_id" : 4, "roundedValue" : -45 }

項目一覧