Cara menggunakan round mongodb

MongoDB provides a variety of arithmetic expression operators. The $round operator is one of those operators. This operator is used to round a number to an integer or specified decimal place.

Syntax of the $round operator:

Important Point:

  1. The < number > can be any valid expression until it resolves to a number. The value of themust be an integer, double, decimal, or long. If theis not of non-numeric data type, it returns an error.
  2. Thecan be any valid optional expression resolves to an integer between -20 and 100. These are the following conditions of thefield:
ConditionExampleOutputIfresolves to a positive integer, it rounds off the value of the decimal side.$round: [7896.1479, 3]7896.15Ifresolves to a negative integer, it rounds off the value of thedigits to the left of the decimal.$round: [7843.1479, -2]7800If the absolute value of < place > is equal to or greater than the number of digits to the left of the decimal, it returns 0.$round: [7875.1479, -5]0If theresolves to 0, it rounds off the first digit to the right of the decimal and returns the rounded integer value.$round: [7894.9879, 0]7894

Examples:

Suppose we have a collection of the products with the following documents.

Example 1

In this example, we are using the $round operator to round off the value in the data field.

Output:

{
    "_id" : 1, 
    "data" : 11.29,
    "value" : 11
}
{
    "_id" : 2, 
    "data" : 10.32,	
    "value" : 10
}
{
    "_id" : 3, 
    "data" : 15.97,	
    "value" : 16
}
{
    "_id" : 4, 
    "data" : -12.38,
    "value" : -12
}
{
    "_id" : 5, 
    "data" : 20.88,
    "value" : 21
}
{
    "_id" : 6, 
    "data" : 5,
    "value" : 5
}
{
    "_id" : 7, 
    "data" : -1,
    "value" : -1
}

Example 2: Specify a decimal place

We can use the second argument to specify how many decimal places the number is to be rounded off. In this example, we are using the $round operator to round off the value in the data field.

Output:

{
    "_id" : 1, 
    "data" : 11.29,
    "value" : 11.3
}
{
    "_id" : 2, 
    "data" : 10.32,	
    "value" : 10.3
}
{
    "_id" : 3, 
    "data" : 15.97,	
    "value" : 16
}
{
    "_id" : 4, 
    "data" : -12.38,
    "value" : -12.4
}
{
    "_id" : 5, 
    "data" : 20.88,
    "value" : 20.9
}
{
    "_id" : 6, 
    "data" : 5,
    "value" : 5
}
{
    "_id" : 7, 
    "data" : -1,
    "value" : -1
}

Suppose we add the following documents to our collection:

{
        {
         "_id" : 8, 
         "name" : "charge",
         "data" : 8611.1325
        }
        {
         "_id" : 9, 
         "name" : "pen",
         "data" : 1843.5632	
        }
        {
         "_id" : 10, 
         "name" : "chart",
         "data" : 5714.1397	
        }
        {
         "_id" : 11, 
         "name" : "glass",
         "data" :  2312.3012
        }
}

Example 3: Negative Decimal Places

In this example, we are using the $round operator to round off the value of the data field with various negative decimal place values.

Output:

{
    "_id" : 8, 
    "data" : 8611.1325,
    "value_1" : 8610,
    "value_2" : 8600
}
{
    "_id" : 9, 
    "data" : 1843.5632,	
    "value_1" : 1840,
    "value_2" : 1800
}
{
    "_id" : 10, 
    "data" : 5714.1397,	
    "value_1" : 5710,
    "value_2" : 5700
}
{
    "_id" : 11, 
    "data" :  2312.3012,
    "value_1" : 2310,
    "value_2" : 2300,
}

Example 4:value is a Zero

When the value of thefield is 0, the $round operator rounds off the first digit to the right of the decimal and returns the rounded value. In this example, we are using the $round operator to round off the value in the data field.

In MongoDB, the

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
6 aggregation pipeline operator rounds a number to a whole integer or to a specified decimal place.

You have the option of specifying how many decimal places for which to round the number. To do this, pass a second argument. The first argument is the number to round, and the second (optional) argument is the number of decimal places to round it to.

Example

Suppose we have a collection called

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
7 with the following documents:

{ "_id" : 1, "data" : 8.99 }
{ "_id" : 2, "data" : 8.45 }
{ "_id" : 3, "data" : 8.451 }
{ "_id" : 4, "data" : -8.99 }
{ "_id" : 5, "data" : -8.45 }
{ "_id" : 6, "data" : -8.451 }
{ "_id" : 7, "data" : 8 }
{ "_id" : 8, "data" : 0 }

We can use the

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
6 operator to round the values in the
{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
9 field:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data" ] }
          }
     }
   ]
)

Result:

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }

Specify a Decimal Place

We have the option of using a second argument to specify how many decimal places for which to round the number.

Example:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data", 1 ] }
          }
     }
   ]
)

Result:

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8.4 }
{ "data" : 8.451, "rounded" : 8.5 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8.4 }
{ "data" : -8.451, "rounded" : -8.5 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }

Negative Decimal Places

The second argument can be any valid expression that resolves to an integer between -20 and 100, exclusive. Therefore, you can specify a negative decimal place.

When you do this, the number is rounded to the left of the decimal place. If the absolute value of the negative integer is greater than the number, the result is

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data", 1 ] }
          }
     }
   ]
)
0.

Suppose we add the following documents to our collection:

{ "_id" : 9, "data" : 8111.32 }
{ "_id" : 10, "data" : 8514.321 }
{ "_id" : 11, "data" : 8999.454 }

Here’s an example of using various negative decimal places when applying

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
6 to those documents:

db.test.aggregate(
   [
     { $match: { _id: { $in: [ 9, 10, 11 ] } } },
     {
       $project:
          {
            _id: 0,
            data: 1,
            a: { $round: [ "$data", -1 ] },
            b: { $round: [ "$data", -2 ] },
            c: { $round: [ "$data", -3 ] },
            d: { $round: [ "$data", -4 ] },
            e: { $round: [ "$data", -5 ] }
          }
     }
   ]
).pretty()

Result:

{
	"data" : 8111.32,
	"a" : 8110,
	"b" : 8100,
	"c" : 8000,
	"d" : 10000,
	"e" : 0
}
{
	"data" : 8514.321,
	"a" : 8510,
	"b" : 8500,
	"c" : 9000,
	"d" : 10000,
	"e" : 0
}
{
	"data" : 8999.454,
	"a" : 9000,
	"b" : 9000,
	"c" : 9000,
	"d" : 10000,
	"e" : 0
}

Decimal Place of Zero

When you provide a decimal place of

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data", 1 ] }
          }
     }
   ]
)
0, the 
{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
6 operator rounds using the first digit to the right of the decimal and returns the rounded integer value.

Example:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data", 0 ] }
          }
     }
   ]
)

Result:

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
{ "data" : 8111.32, "rounded" : 8111 }
{ "data" : 8514.321, "rounded" : 8514 }
{ "data" : 8999.454, "rounded" : 8999 }

Number Types

The number to round can be any valid expression that resolves to an integer, double, decimal, or long. The return value matches the data type of the input value.

So if we add the following documents to our collection:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data" ] }
          }
     }
   ]
)
0

We can apply

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
6 to the
{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
9 field:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data" ] }
          }
     }
   ]
)
1

Result:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data" ] }
          }
     }
   ]
)
2

Rounding to Null Decimal Places

If the second argument is

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data", 1 ] }
          }
     }
   ]
)
6, the result is
db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data", 1 ] }
          }
     }
   ]
)
6.

Example:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data" ] }
          }
     }
   ]
)
3

Result:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data" ] }
          }
     }
   ]
)
4

Rounding a Null Value

If the value to be rounded is

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data", 1 ] }
          }
     }
   ]
)
6, the result is
db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data", 1 ] }
          }
     }
   ]
)
6.

Suppose we add the following document to the collection:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data" ] }
          }
     }
   ]
)
5

And we use

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
6 to round the null value:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data" ] }
          }
     }
   ]
)
6

Result:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data" ] }
          }
     }
   ]
)
7

Rounding Infinity

If the number to be rounded is

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8.4 }
{ "data" : 8.451, "rounded" : 8.5 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8.4 }
{ "data" : -8.451, "rounded" : -8.5 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
1, the result is
{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8.4 }
{ "data" : 8.451, "rounded" : 8.5 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8.4 }
{ "data" : -8.451, "rounded" : -8.5 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
1. Likewise, if it’s
{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8.4 }
{ "data" : 8.451, "rounded" : 8.5 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8.4 }
{ "data" : -8.451, "rounded" : -8.5 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
3, the result is
{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8.4 }
{ "data" : 8.451, "rounded" : 8.5 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8.4 }
{ "data" : -8.451, "rounded" : -8.5 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
3.

Let’s add two documents with such values:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data" ] }
          }
     }
   ]
)
8

And let’s round them:

db.test.aggregate(
   [
     {
       $project:
          {
            _id: 0,
            data: 1,
            rounded: { $round: [ "$data" ] }
          }
     }
   ]
)
9

Result:

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
0

Rounding NaN

Rounding

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8.4 }
{ "data" : 8.451, "rounded" : 8.5 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8.4 }
{ "data" : -8.451, "rounded" : -8.5 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
5 results in
{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8.4 }
{ "data" : 8.451, "rounded" : 8.5 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8.4 }
{ "data" : -8.451, "rounded" : -8.5 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
5.

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
1

Result:

{ "data" : 8.99, "rounded" : 9 }
{ "data" : 8.45, "rounded" : 8 }
{ "data" : 8.451, "rounded" : 8 }
{ "data" : -8.99, "rounded" : -9 }
{ "data" : -8.45, "rounded" : -8 }
{ "data" : -8.451, "rounded" : -8 }
{ "data" : 8, "rounded" : 8 }
{ "data" : 0, "rounded" : 0 }
2

Non-Numeric Types

If you try to round a value that’s the wrong data type (i.e. it isn’t an integer, double, decimal, or long), an error is returned.