MongoDB $trunc Operator

What is the $trunc operator in MongoDB?

MongoDB provides a variety of arithmetic expression operators. The $trunc operator is one of those operators. The $trunc operator is used to truncate a number to a whole number or specified decimal place.

Syntax of the $trunc 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.
ConditionExampleOutput
If theresolves to a positive integer, it truncates to thevalue of decimal places.$trunc: [7896.12569, 3]7896.125
If theresolves to a negative integer, it replacesdigits left of the decimal with 0.$trunc: [7896.12569, -3]7000
$trunc: [7896.12569, -5]0
If theresolves to 0, it truncates all digits to the right of the decimal and returns the whole integer value.$trunc: [7896.12569, 0]7896

Examples:

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

Example 1

In this example, we are using the $trunc operator to truncate the values in the data field.

Output:

{
    "_id" : 1, 
    "data" : 11.25,
    "value" : 11
}
{
    "_id" : 2, 
    "data" : 10.32,	
    "value" : 10
}
{
    "_id" : 3, 
    "data" : 15.97,	
    "value" : 15
}
{
    "_id" : 4, 
    "data" : -12.3,
    "value" : -12
}
{
    "_id" : 5, 
    "data" : 20.6,
    "value" : 20
}
{
    "_id" : 6, 
    "data" : 5,
    "value" : 5
}
{
    "_id" : 7, 
    "data" : -1,
    "value" : -1
}

Example 2: Specify decimal place truncate

In this example, we are using the $trunc operator to truncate the values in the data field.

Output:

{
    "_id" : 1, 
    "data" : 11.25,
    "value" : 11.2
}
{
    "_id" : 2, 
    "data" : 10.32,	
    "value" : 10.3
}
{
    "_id" : 3, 
    "data" : 15.97,	
    "value" : 15.9
}
{
    "_id" : 4, 
    "data" : -12.3,
    "value" : -12.3
}
{
    "_id" : 5, 
    "data" : 20.6,
    "value" : 20.6
}
{
    "_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" : 1856.5632	
        }
        {
         "_id" : 10, 
         "name" : "chart",
         "data" : 5785.1397	
        }
        {
         "_id" : 11, 
         "name" : "glass",
         "data" :  2312.3012
        }
}

Example 3: Negative Decimal Places

In this example, we are using the $trunc operator to truncate the values in the data field.

Output:

{
    "_id" : 8, 
    "data" : 8611.1325,
    "value_1" : 8610,
    "value_2" : 8600,
    "value_3" : 8000,
    "value_4" : 0
}
{
    "_id" : 9, 
    "data" : 1856.5632,	
    "value_1" : 1850,
    "value_2" : 1800,
    "value_3" : 1000,
    "value_4" : 0
}
{
    "_id" : 10, 
    "data" : 5785.1397,	
    "value_1" : 5780,
    "value_2" : 5700,
    "value_3" : 5000,
    "value_4" : 0
}
{
    "_id" : 11, 
    "data" :  2312.3012,
    "value_1" : 2310,
    "value_2" : 2300,
    "value_3" : 2000,
    "value_4" : 0
}





Latest Courses