MongoDB $round vs $trunc: What’s the Difference?

MongoDB’s aggregation pipeline framework includes a $round operator and a $trunc operator. These operators perform similar, but different tasks.

Definitions

First, let’s look at the definitions of each operator:

  • The $round operator rounds a number to a whole integer or to a specified decimal place.
  • The $truncate operator truncates a number to a whole integer or to a specified decimal place.

Basically, the difference is in the words round vs truncate.

In some cases, both operators will return the same result. In other cases, their results will differ. This is because the $round operator may round the number up, depending on the value. The $truncate operator doesn’t round the number. Instead, it simply truncates it. In other words, it simply cuts the number as specified, while leaving the remaining digits as they are.

Example

Suppose we have a collection called test 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 }
{ "_id" : 9, "data" : 0.5 }
{ "_id" : 10, "data" : 8111.32 }
{ "_id" : 11, "data" : 8514.321 }
{ "_id" : 12, "data" : 8999.454 }

Here’s what happens when we apply $round and $truncate to those documents:

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

Result:

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

We can see that in some cases, the result is the same. In others, it’s different. For example, when the input value is 0.9, the $round operator rounds the number up to 1. The $truncate operator on the other hand simply removes the .9 part, which produces a result of 0.

Negative Fractional Places

Both operators accept an optional second argument. When present, this argument specifies the number of decimal places to round/truncate the number to.

Providing this second argument can further highlight the difference between the two operators.

Example:

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

Result:

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

Again we can see that some results are identical while others are not.

Negative Fractional Places

Both operators accept a negative value for the second argument.

Example:

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

Result:

{ "data" : 0, "rounded" : 0, "truncated" : 0 }
{ "data" : 8, "rounded" : 10, "truncated" : 0 }
{ "data" : 0.5, "rounded" : 0, "truncated" : 0 }
{ "data" : 0.9, "rounded" : 0, "truncated" : 0 }
{ "data" : 8.99, "rounded" : 10, "truncated" : 0 }
{ "data" : 8.45, "rounded" : 10, "truncated" : 0 }
{ "data" : 8.451, "rounded" : 10, "truncated" : 0 }
{ "data" : -8.99, "rounded" : -10, "truncated" : 0 }
{ "data" : -8.45, "rounded" : -10, "truncated" : 0 }
{ "data" : -8.451, "rounded" : -10, "truncated" : 0 }

This time there’s a stark contrast between the results produced by the two operators. The $trunc operator produced 0 for every document, while the $round operator returned various values, most of which were rounded up or down.

$floor and $ceil

Two more operators to be aware of when performing operations like this are $floor and $ceil. These operators work in a similar fashion, but slightly different.

  • $floor returns the largest integer less than or equal to the specified number
  • $ceil returns the smallest integer greater than or equal to the specified number.