MongoDB $round Operator

What is the $round operator in 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 the <number> must be an integer, double, decimal, or long. If the <number> is not of non-numeric data type, it returns an error.
  2. The <place> can be any valid optional expression resolves to an integer between -20 and 100. These are the following conditions of the <place> field:
ConditionExampleOutput
If <place> resolves to a positive integer, it rounds off the value of the decimal side.$round: [7896.1479, 3]7896.15
If <place> resolves to a negative integer, it rounds off the value of the <place> digits to the left of the decimal.$round: [7843.1479, -2]7800
If 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]0
If the <place> resolves 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: <Place> value is a Zero

When the value of the <place> field 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.

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
}
{
    "_id" : 8, 
    "data" : 8611.1325,
    "value" : 8611     
}
{
    "_id" : 9, 
    "data" : 1843.5632,	
    "value" : 1843     
}
{
    "_id" : 10, 
    "data" : 5714.1397,	
    "value" : 5714    	
}
{
    "_id" : 11, 
    "data" :  2312.3012,	
    "value" : 2312   
}





Latest Courses