$trunc (aggregation)

Definition

  • $trunc

Changed in version 4.2..

$trunc truncates a number to a whole integer or to aspecified decimal place.

MongoDB 4.2 adds the following syntax for $trunc:

  1. { $trunc : [ <number>, <place> ] }

FieldTypeDescription<number>numberCan be any valid expressionthat resolves to a number. Specifically, the expression mustresolve to an integer, double,decimal, orlong.

$trunc returns an error if the expressionresolves to a non-numeric data type.<place>integerOptional Can be any validexpression that resolves toan integer between -20 and 100, exclusive. e.g.-20 < place < 100. Defaults to 0 if unspecified.

  • If <place> resolves to a positive integer,$trunc truncates to <place> decimalplaces.

For example, $trunc : [1234.5678, 2] truncates to twodecimal places and returns 1234.56.

  • If <place> resolves to a negative integer,$trunc replaces <place> digits left ofthe decimal with 0.

For example, $trunc : [1234.5678, -2] replaces to twodigits left of the decimal with 0 and returns 1200.

If the absolute value of <place> exceeds the number ofdigits to the left of the decimal, $truncreturns 0.

For example, $trunc : [ 1234.5678, -5] specifies thefifth digit left of the decimal. This exceeds thenumber of digits left of the decimal and returns 0.

  • If <place> resolves to 0, trunctruncates all digits to the right of the decimal and returnsthe whole integer value.

For example, $trunc : [1234.5678, 0] returns 1234

Prior to MongoDB 4.2, $trunc truncated the input valueto the whole integer. MongoDB 4.2 continues supporting the pre-4.2syntax and behavior:

  1. { $trunc: <number> }

The <number> expression can be any valid expression as long as it resolves to a number. Formore information on expressions, see Expressions.

Behavior

$trunc does not round the truncated data. To roundinput values to a specified place, use the$round expression.

Returned Data Type

If truncating to a specific decimal place, the data type returned by$trunc matches the data type of the input expression orvalue.

If truncating to a whole integer value, $trunc returnsan integer.

null, NaN, and +/- Infinity

  • If the argument resolves to a value of null or refers to a fieldthat is missing, $trunc returns null.
  • If the argument resolves to NaN, $trunc returnsNaN.
  • If the argument resolves to negative or positive infinity,$trunc returns negative or positive infinityrespectively.
ExampleResults
{ $trunc: [ NaN, 1] }NaN
{ $trunc: [ null, 1] }null
{ $trunc : [ Infinity, 1 ] }Infinity
{ $trunc : [ -Infinity, 1 ] }-Infinity

Example

A collection named samples contains the following documents:

  1. { _id: 1, value: 19.25 }
  2. { _id: 2, value: 28.73 }
  3. { _id: 3, value: 34.32 }
  4. { _id: 4, value: -45.34 }
  • The following aggregation returns value truncated to the firstdecimal place:
  1. db.samples.aggregate([
  2. { $project: { truncatedValue: { $trunc: [ "$value", 1 ] } } }
  3. ])

The operation returns the following results:

  1. { "_id" : 1, "truncatedValue" : 19.2 }
  2. { "_id" : 2, "truncatedValue" : 28.7 }
  3. { "_id" : 3, "truncatedValue" : 34.3 }
  4. { "_id" : 4, "truncatedValue" : -45.3 }
  • The following aggregation returns value truncated to the firstplace:
  1. db.samples.aggregate([
  2. { $project: { truncatedValue: { $trunc: [ "$value", -1 ] } } }
  3. ])

The operation returns the following results:

  1. { "_id" : 1, "truncatedValue" : 10 }
  2. { "_id" : 2, "truncatedValue" : 20 }
  3. { "_id" : 3, "truncatedValue" : 30 }
  4. { "_id" : 4, "truncatedValue" : -40 }
  • The following aggregation returnsvalue truncated to the wholeinteger:
  1. db.samples.aggregate([
  2. { $project: { truncatedValue: { $trunc: [ "$value", 0 ] } } }
  3. ])

The operation returns the following results:

  1. { "_id" : 1, "truncatedValue" : 19 }
  2. { "_id" : 2, "truncatedValue" : 28 }
  3. { "_id" : 3, "truncatedValue" : 34 }
  4. { "_id" : 4, "truncatedValue" : -45 }