$toInt (aggregation)

Definition

  • $toInt

New in version 4.0.

Converts a value to an integer. If the value cannot be convertedto an integer, $toInt errors. If the value is null ormissing, $toInt returns null.

$toInt has the following syntax:

  1. {
  2. $toInt: <expression>
  3. }

The $toInt takes any valid expression.

The $toInt is a shorthand for the following$convert expression:

  1. { $convert: { input: <expression>, to: "int" } }

See also

$convert

Behavior

The following table lists the input types that can be converted to aninteger:

Input TypeBehavior
BooleanReturns 0 for false.Returns 1 for true.
DoubleReturns truncated value.The truncated double value must fall within the minimum andmaximum value for an integer.You cannot convert a double value whose truncated value is lessthan the minimum integer value or is greater than the maximuminteger value.
DecimalReturns truncated value.The truncated decimal value must fall within the minimum andmaximum value for an integer.You cannot convert a decimal value whose truncated value is lessthan the minimum integer value or is greater than the maximuminteger value.
IntegerNo-op. Returns the integer value.
LongReturns the long value as an integer.The long value must fall within the minimum and maximum valuefor an integer.You cannot convert a long value that is less than the minimuminteger value or is greater than the maximum integer value.
StringReturns the numerical value of the string as an integer.The string value must be a base10 integer; e.g."-5", "123456").You cannot convert a string value of a float or decimal ornon-base10 number (e.g. "-5.0", "0x6400")

The following table lists some conversion to integer examples:

ExampleResults
$toInt: true1
$toInt: false0
$toInt: 1.999991
$toInt: NumberDecimal("5.5000")5
$toInt: NumberDecimal("9223372036000.000")Error
$toInt: NumberLong("5000")5000
$toInt: NumberLong("922337203600")Error
$toInt: "-2"-2
$toInt: "2.5"Error
$toInt: nullnull

Example

Create a collection orders with the following documents:

  1. db.orders.insert( [
  2. { _id: 1, item: "apple", qty: 5, price: 10 },
  3. { _id: 2, item: "pie", qty: 10, price: NumberDecimal("20.0") },
  4. { _id: 3, item: "ice cream", qty: 2, price: "4.99" },
  5. { _id: 4, item: "almonds" , qty: 5, price: 5 }
  6. ] )

The following aggregation operation on the orders collectionconverts the qty to an integer as well as convert price to adecimal before calculating the total price:

  1. // Define stage to add convertedPrice and convertedQty fields with the converted price and qty values
  2.  
  3. priceQtyConversionStage = {
  4. $addFields: {
  5. convertedPrice: { $toDecimal: "$price" },
  6. convertedQty: { $toInt: "$qty" },
  7. }
  8. };
  9.  
  10. // Define stage to calculate total price by multiplying convertedPrice and convertedQty fields
  11.  
  12.  
  13. totalPriceCalculationStage = {
  14. $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }
  15. };
  16.  
  17. db.orders.aggregate( [
  18. priceQtyConversionStage,
  19. totalPriceCalculationStage
  20. ])

The operation returns the following documents:

  1. { "_id" : 1, "item" : "apple", "totalPrice" : NumberDecimal("50.0000000000000") }
  2. { "_id" : 2, "item" : "pie", "totalPrice" : NumberDecimal("200.0") }
  3. { "_id" : 3, "item" : "ice cream", "totalPrice" : NumberDecimal("9.98") }
  4. { "_id" : 4, "item" : "almonds", "totalPrice" : NumberDecimal("25.00000000000000") }

Note

If the conversion operation encounters an error, the aggregationoperation stops and throws an error. To override this behavior, use$convert instead.