$toInt (aggregation)
On this page
Definition
$toInt
Converts a value to an integer. If the value cannot be converted to an integer,
$toInt
errors. If the value is null or missing,$toInt
returns null.$toInt
has the following syntax:{ $toInt: <expression> } The
$toInt
takes any valid expression.The
$toInt
is a shorthand for the following$convert
expression:{ $convert: { input: <expression>, to: "int" } }
Behavior
The following table lists the input types that can be converted to an integer:
Input Type | Behavior |
---|---|
Boolean | Returns 0 for false .Returns 1 for true . |
Double | Returns truncated value. The truncated double value must fall within the minimum and maximum value for an integer. You cannot convert a double value whose truncated value is less than the minimum integer value or is greater than the maximum integer value. |
Decimal | Returns truncated value. The truncated decimal value must fall within the minimum and maximum value for an integer. You cannot convert a decimal value whose truncated value is less than the minimum integer value or is greater than the maximum integer value. |
Integer | No-op. Returns the integer value. |
Long | Returns the long value as an integer. The long value must fall within the minimum and maximum value for an integer. You cannot convert a long value that is less than the minimum integer value or is greater than the maximum integer value. |
String | Returns the numerical value of the string as an integer. The string value must be a base 10 integer; e.g.
You cannot convert a string value of a float or decimal or
non-base 10 number (e.g. |
The following table lists some conversion to integer examples:
Example | Results |
---|---|
$toInt: true | 1 |
$toInt: false | 0 |
$toInt: 1.99999 | 1 |
$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: null | null |
Example
Create a collection orders
with the following documents:
db.orders.insertMany( [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal("20.0") }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 } ] )
The following aggregation operation:
converts
qty
to an integer,converts
price
to a decimal,calculates the total price:
// Define stage to add convertedPrice and convertedQty fields with the converted price and qty values priceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, } }; // Define stage to calculate total price by multiplying convertedPrice and convertedQty fields totalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } } }; db.orders.aggregate( [ priceQtyConversionStage, totalPriceCalculationStage ] )
The operation returns the following documents:
{ _id: 1, item: 'apple', totalPrice: Decimal128("50") }, { _id: 2, item: 'pie', totalPrice: Decimal128("200.0") }, { _id: 3, item: 'ice cream', totalPrice: Decimal128("9.98") }, { _id: 4, item: 'almonds', totalPrice: Decimal128("25") }
Note
If the conversion operation encounters an error, the aggregation
operation stops and throws an error. To override this behavior, use
$convert
instead.