Introduction to MongoDB $trunc Operator
$trunc
is a MongoDB aggregation pipeline operator used to truncate a number to a specified number of decimal places or integer digits. It takes one or two parameters, the first being the number to be truncated and the second being the number of decimal places to keep (optional, defaults to 0). If the specified number of decimal places is positive, it indicates truncation of decimal places, and if negative, it indicates truncation of integer digits.
Syntax
The syntax of the $trunc
operator is as follows:
{ $trunc: { <number>, <numDigits> } }
Here, <number>
is the number to be truncated and can be any numeric expression, such as a field name, numeric literal, or other aggregation expression, and <numDigits>
is the number of decimal places to keep, which can be an integer or a numeric expression.
Use Cases
The $trunc
operator is typically used in scenarios where a number needs to be truncated to a specified number of digits, such as rounding monetary data to a specified number of decimal places or truncating latitude and longitude data to a specified precision.
Examples
Example Data
Assume there is a collection named sales
that stores sales data, as shown below:
{ "_id" : 1, "product" : "A", "amount" : 1234.5678 }
{ "_id" : 2, "product" : "B", "amount" : 987.6543 }
{ "_id" : 3, "product" : "C", "amount" : 456.7890 }
Example 1
The following aggregation pipeline uses the $trunc
operator to truncate the value of the amount
field to two decimal places:
db.sales.aggregate([
{
$project: {
product: 1,
truncatedAmount: { $trunc: ["$amount", 2] }
}
}
])
After running the above aggregation pipeline, the following result is obtained:
{ "_id" : 1, "product" : "A", "truncatedAmount" : 1234.56 }
{ "_id" : 2, "product" : "B", "truncatedAmount" : 987.65 }
{ "_id" : 3, "product" : "C", "truncatedAmount" : 456.78 }
In the above result, the value of the amount
field is truncated to two decimal places, and the truncated result is saved in the new field truncatedAmount
.
Example 2
The following aggregation pipeline uses the $trunc
operator to truncate the value of the amount
field to an integer:
db.sales.aggregate([
{
$project: {
product: 1,
truncatedAmount: { $trunc: ["$amount", 0] }
}
}
])
After running the above aggregation pipeline, the following result is obtained:
{ "_id" : 1, "product" : "A", "truncatedAmount" : 1234 }
{ "_id" : 2, "product" : "B", "truncatedAmount" : 987 }
{ "_id" : 3, "product" : "C", "truncatedAmount" : 456 }
Conclusion
In this article, we have learned about the usage and examples of the $trunc
operator. The $trunc
operator is used to truncate a number to a specified number of decimal places or integer digits. It is very useful in the MongoDB aggregation pipeline because it allows us to process numerical data with a specified precision.
When using the $trunc
operator, the following points should be noted:
- The
$trunc
operator can only be applied to fields of numeric type. - The
$trunc
operator must specify the number of decimal or integer places to truncate. - The
$trunc
operator always returns a number, even if the input is not a numeric type.
Overall, the $trunc
operator is a very useful operator in the MongoDB aggregation pipeline that can help us process numerical data with a specified precision.