In MongoDB, the $trunc
aggregation pipeline operator truncates 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 truncate the number. To do this, pass a second argument. The first argument is the number to truncate, and the second (optional) argument is the number of decimal places to truncate it to.
Omitting the second argument truncates all digits to the right of the decimal and returns the whole integer value.
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 }
We can use the $trunc
operator to truncate the values in the data
field:
db.test.aggregate(
[
{
$project:
{
_id: 0,
data: 1,
truncated: { $trunc: [ "$data" ] }
}
}
]
)
Result:
{ "data" : 8.99, "truncated" : 8 } { "data" : 8.45, "truncated" : 8 } { "data" : 8.451, "truncated" : 8 } { "data" : -8.99, "truncated" : -8 } { "data" : -8.45, "truncated" : -8 } { "data" : -8.451, "truncated" : -8 } { "data" : 8, "truncated" : 8 } { "data" : 0, "truncated" : 0 }
Notice that $trunc
doesn’t round numbers like $round
does. The $trunc
operator simply truncates the number. If we had applied $round
to this collection, the first and fourth documents would have been rounded to 9
and -9
respectively.
Specify a Decimal Place
We have the option of using a second argument to specify how many decimal places to truncate the number to.
Example:
db.test.aggregate(
[
{
$project:
{
_id: 0,
data: 1,
truncated: { $trunc: [ "$data", 1 ] }
}
}
]
)
Result:
{ "data" : 8.99, "truncated" : 8.9 } { "data" : 8.45, "truncated" : 8.4 } { "data" : 8.451, "truncated" : 8.4 } { "data" : -8.99, "truncated" : -8.9 } { "data" : -8.45, "truncated" : -8.4 } { "data" : -8.451, "truncated" : -8.4 } { "data" : 8, "truncated" : 8 } { "data" : 0, "truncated" : 0 }
Again, this simply truncates the number. If we’d used $round
, it would have rounded some of these numbers.
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 truncated to the left of the decimal place. If the absolute value of the negative integer is greater the number of digits to the left of the decimal, the result is 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 $trunc
to those documents:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 9, 10, 11 ] } } },
{
$project:
{
_id: 0,
data: 1,
a: { $trunc: [ "$data", -1 ] },
b: { $trunc: [ "$data", -2 ] },
c: { $trunc: [ "$data", -3 ] },
d: { $trunc: [ "$data", -4 ] },
e: { $trunc: [ "$data", -5 ] }
}
}
]
).pretty()
Result:
{ "data" : 8111.32, "a" : 8110, "b" : 8100, "c" : 8000, "d" : 0, "e" : 0 } { "data" : 8514.321, "a" : 8510, "b" : 8500, "c" : 8000, "d" : 0, "e" : 0 } { "data" : 8999.454, "a" : 8990, "b" : 8900, "c" : 8000, "d" : 0, "e" : 0 }
Decimal Place of Zero
When you provide a decimal place of 0
, the $trunc
operator truncates all digits to the right of the decimal and returns the whole integer value.
Example:
db.test.aggregate(
[
{
$project:
{
_id: 0,
data: 1,
truncated: { $trunc: [ "$data", 0 ] }
}
}
]
)
Result:
{ "data" : 8.99, "truncated" : 8 } { "data" : 8.45, "truncated" : 8 } { "data" : 8.451, "truncated" : 8 } { "data" : -8.99, "truncated" : -8 } { "data" : -8.45, "truncated" : -8 } { "data" : -8.451, "truncated" : -8 } { "data" : 8, "truncated" : 8 } { "data" : 0, "truncated" : 0 } { "data" : 8111.32, "truncated" : 8111 } { "data" : 8514.321, "truncated" : 8514 } { "data" : 8999.454, "truncated" : 8999 }
Number Types
The number to truncate 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:
{ "_id" : 12, "data" : NumberDecimal("128.4585") } { "_id" : 13, "data" : NumberDecimal("128.12345678912") }
We can apply $trunc
to the data
field:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 12, 13 ] } } },
{
$project:
{
_id: 0,
data: 1,
a: { $trunc: [ "$data", -1 ] },
b: { $trunc: [ "$data", 0 ] },
c: { $trunc: [ "$data", 3 ] },
d: { $trunc: [ "$data", 4 ] },
e: { $trunc: [ "$data", 5 ] }
}
}
]
).pretty()
Result:
{ "data" : NumberDecimal("128.4585"), "a" : NumberDecimal("1.2E+2"), "b" : NumberDecimal("128"), "c" : NumberDecimal("128.458"), "d" : NumberDecimal("128.4585"), "e" : NumberDecimal("128.45850") } { "data" : NumberDecimal("128.12345678912"), "a" : NumberDecimal("1.2E+2"), "b" : NumberDecimal("128"), "c" : NumberDecimal("128.123"), "d" : NumberDecimal("128.1234"), "e" : NumberDecimal("128.12345") }
Truncating to Null Decimal Places
If the second argument is null
, the result is null
.
Example:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 1, 2, 3 ] } } },
{
$project:
{
_id: 0,
data: 1,
truncated: { $trunc: [ "$data", null ] }
}
}
]
)
Result:
{ "data" : 8.99, "truncated" : null } { "data" : 8.45, "truncated" : null } { "data" : 8.451, "truncated" : null }
Truncating a Null Value
If the value to be truncated is null
, the result is null
.
Suppose we add the following document to the collection:
{ "_id" : 14, "data" : null }
And we use $trunc
to truncate the null value:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 14 ] } } },
{
$project:
{
_id: 0,
data: 1,
truncated: { $trunc: [ "$data", null ] }
}
}
]
)
Result:
{ "data" : null, "truncated" : null }
Truncating Infinity
If the number to be truncated is Infinity
, the result is Infinity
. Likewise, if it’s -Infinity
, the result is -Infinity
.
Let’s add two documents with such values:
{ "_id" : 15, "data" : Infinity } { "_id" : 16, "data" : -Infinity }
And let’s truncate them:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 15, 16 ] } } },
{
$project:
{
_id: 0,
data: 1,
truncated: { $trunc: [ "$data", 2 ] }
}
}
]
)
Result:
{ "data" : Infinity, "truncated" : Infinity } { "data" : -Infinity, "truncated" : -Infinity }
Truncating NaN
Truncating NaN
results in NaN
.
db.test.aggregate(
[
{ $match: { _id: { $in: [ 1, 2 ] } } },
{
$project:
{
_id: 0,
data: 1,
truncated: { $trunc: [ "$data" * 2 ] }
}
}
]
)
Result:
{ "data" : 8.99, "truncated" : NaN } { "data" : 8.45, "truncated" : NaN }
Non-Numeric Types
If you try to truncate a value that’s the wrong data type (i.e. it isn’t an integer, double, decimal, or long), an error is returned.
Suppose we add the following document to our collection:
{ "_id" : 17, "data" : "Thirty five" }
And now we try to truncate the data
field:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 17 ] } } },
{
$project:
{
_id: 0,
data: 1,
truncated: { $trunc: [ "$data" ] }
}
}
]
)
Result:
uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "$trunc only supports numeric types, not string", "code" : 51081, "codeName" : "Location51081" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:639:17 assert.commandWorked@src/mongo/shell/assert.js:729:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12 @(shell):1:1