$toDecimal (aggregation)

On this page本页内容

Definition定义

$toDecimal

New in version 4.0.版本4.0中的新功能。

Converts a value to a decimal. If the value cannot be converted to a decimal, $toDecimal errors. 将值转换为十进制。如果该值无法转换为十进制,则$toDecimal发生错误。If the value is null or missing, $toDecimal returns null.如果值为null或缺失,$toDecimal返回null

$toDecimal has the following syntax:语法如下所示:

{
   $toDecimal: <expression>
}

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

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

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

See also参阅

$convert

Behavior行为

The following table lists the input types that can be converted to a decimal:下表列出了可以转换为十进制的输入类型:

Input Type输入类型Behavior行为
Boolean
Returns NumberDecimal("0") for false.对于false返回NumberDecimal("0")
Returns NumberDecimal("1") for true.对于true返回NumberDecimal("1")
Double Returns double value as a decimal.以十进制形式返回双精度值。
Decimal No-op. Returns the decimal.No-op.返回小数点。
Integer Returns the int value as a decimal.以十进制形式返回int值。
Long Returns the long value as a decimal.以十进制形式返回长值。
String

Returns the numerical value of the string as a decimal.以十进制形式返回字符串的数值。

The string value must be of a base10 numeric value (e.g. "-5.5", "123456").字符串值必须是base10"-5.5"、"123456")。

You cannot convert a string value of a non-base10 number (e.g. "0x6400")不能转换非base10数字的字符串值(例如"0x6400"

Date Returns the number of milliseconds since the epoch that corresponds to the date value.返回自与日期值相对应的历元起的毫秒数。

The following table lists some conversion to decimal examples:下表列出了一些转换为十进制的示例:

Example示例Results结果
{$toDecimal: true} NumberDecimal(“1”)
{$toDecimal: false} NumberDecimal(“0”)
{$toDecimal: 2.5} NumberDecimal(“2.50000000000000”)
{$toDecimal: NumberInt(5)} NumberDecimal(“5”)
{$toDecimal: NumberLong(10000)} NumberDecimal(“10000”)
{$toDecimal: "-5.5"} NumberDecimal(“-5.5”)
{$toDecimal: ISODate("2018-03-27T05:04:47.890Z")} NumberDecimal(“1522127087890”)

Example示例

Create a collection orders with the following documents:使用以下文档创建集合orders

db.orders.insert( [
   { _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 on the orders collection converts the price to a decimal and the qty to an integer before calculating the total price:在计算总价之前,orders集合上的以下聚合操作将price转换为十进制,将qty转换为整数:

// 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" : NumberDecimal("50.0000000000000") }
{ "_id" : 2, "item" : "pie", "totalPrice" : NumberDecimal("200.0") }
{ "_id" : 3, "item" : "ice cream", "totalPrice" : NumberDecimal("9.98") }
{ "_id" : 4, "item" : "almonds", "totalPrice" : NumberDecimal("25.00000000000000") }

Note

If the conversion operation encounters an error, the aggregation operation stops and throws an error. 如果转换操作遇到错误,聚合操作将停止并抛出错误。To override this behavior, use $convert instead.若要重写此行为,请改用$convert