Database Manual / Reference / Query Language / Expressions

$toInt (expression operator)(表达式运算符)

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错误。如果该值为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" } }

Behavior行为

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

Input Type输入类型Behavior行为
Boolean布尔值Returns 0 for false.false返回0
Returns 1 for true.true返回1
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. "-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")您不能转换浮点数、十进制数或非十进制数的字符串值(例如"-5.0""0x6400"

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

Example示例Results结果
$toInt: true1
$toInt: false0
$toInt: 1.999991
$toInt: Decimal128("5.5000")5
$toInt: Decimal128("9223372036000.000")Error
$toInt: Long("5000")5000
$toInt: Long("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: Decimal128("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<定义阶段以添加转换价格和转换数量字段,其中包含转换价格和数量值</span>

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.如果转换操作遇到错误,聚合操作将停止并抛出错误。要覆盖此行为,请改用$converter