Docs HomeMongoDB Manual

$toInt (aggregation)

On this page本页内容

Definition定义

$toInt

Converts a value to an integer. 将值转换为整数。If the value cannot be converted to an integer, $toInt errors. 如果该值无法转换为整数,则$toInt将出错。If the value is null or missing, $toInt returns null.如果该值为null或缺失,$toInt将返回null

$toInt has the following syntax:具有以下语法:

{
$toInt: <expression>
}

The $toInt takes any valid expression.$toInt接受任何有效的表达式

The $toInt is a shorthand for the following $convert expression:$toInt是以下$convert表达式的简写:

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

See also: 另请参阅:

$convert

Behavior行为

The following table lists the input types that can be converted to an integer:下表列出了可以转换为整数的输入类型:

Input Type输入类型Behavior行为
BooleanReturns 0 for false.如果为false,则返回0。
Returns 1 for true.如果为true,则返回1
DoubleReturns 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. 不能转换截断值小于最小整数值或大于最大整数值的双精度值。
DecimalReturns 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. 不能转换截断值小于最小整数值或大于最大整数值的十进制值。
IntegerNo-op. Returns the integer value.没有。返回整数值。
LongReturns 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. 不能转换小于最小整数值或大于最大整数值的长值。
StringReturns the numerical value of the string as an integer.以整数形式返回字符串的数值。
The string value must be a base 10 integer; e.g. "-5", "123456").字符串值必须是以10为基数的整数;例如"-5""123456")。
You cannot convert a string value of a float or decimal or non-base 10 number (e.g. "-5.0", "0x6400") 不能转换浮点、十进制或非基数10的字符串值(例如"-5.0""0x6400"

The following table lists some conversion to integer examples:下表列出了一些转换为整数的示例:

Example示例Results结果
$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:使用以下文档创建集合orders

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,qty转换为整数,
  • converts price to a decimal,price转换为十进制,
  • calculates the total price:计算总价:
// Define stage to add convertedPrice and convertedQty fields with the converted price and qty values定义阶段以添加具有转换后的价格和数量值的convertedPrice和convertedQty字段

priceQtyConversionStage = {
$addFields: {
convertedPrice: { $toDecimal: "$price" },
convertedQty: { $toInt: "$qty" },
}
};

// Define stage to calculate total price by multiplying convertedPrice and convertedQty fields定义阶段以通过乘以convertedPrice和convertedQty字段来计算总价


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.若要覆盖此行为,请改用$convert