$toDouble(aggregation)

On this page本页内容

Definition定义

$toDouble

New in version 4.0.在版本4.0中新增

Converts a value to a double. 将值转换为doubleIf the value cannot be converted to an double, $toDouble errors. 如果无法将值转换为double,则$toDouble错误。If the value is null or missing, $toDouble returns null.如果值为null或缺失,$toDouble将返回null

$toDouble has the following syntax:语法如下:

{
   $toDouble: <expression>
}

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

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

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

Behavior行为

The following table lists the input types that can be converted to a double:下表列出了可以转换为双精度类型的输入类型:

Input Type输入类型Behavior行为
Boolean

Returns 0 for false.如果为false,则返回0

Returns 1 for true.如果为true,则返回1

DoubleNo-op. Returns the double.无。返回双精度值。
Decimal

Returns the decimal value as a double.以双精度形式返回十进制值。

The decimal value must fall within the minimum and maximum value for a double.十进制值必须在双精度数的最小值和最大值之间。

You cannot convert a decimal value whose value is less than the minimum double value or is greater than the maximum double value.不能转换值小于最小双精度值或大于最大双精度值的十进制值。

IntegerReturns the int value as a double.double形式返回int值。
LongReturns the long value as a double.double形式返回长值。
String

Returns the numerical value of the string as a double.以双精度形式返回字符串的数值。

The string value must be of a base 10 numeric value (e.g. "-5.5", "123456") and fall within the minimum and maximum value for a double.字符串值必须是以10为基数的数值(例如"-5.5""123456"),并且必须在双精度数的最小值和最大值范围内。

You cannot convert a string value of a non-base 10 number (e.g. "0x6400") or a value that falls outside the minimum and maximum value for a double.不能转换非十进制数字(例如"0x6400")的字符串值或超出双精度数最小值和最大值的值。

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

The following table lists some conversion to double examples:下表列出了一些转换为双重示例:

Example示例Results结果
$toDouble: true1
$toDouble: false0
$toDouble: 2.52.5
$toDouble: NumberInt(5)5
$toDouble: NumberLong(10000)10000
$toDouble: "-5.5"-5.5
$toDouble: ISODate("2018-03-27T05:04:47.890Z")1522127087890

Example示例

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

db.weather.insertMany( [
   { _id: 1, date: new Date("2018-06-01"), temp: "26.1C" },
   { _id: 2,  date: new Date("2018-06-02"), temp: "25.1C" },
   { _id: 3,  date: new Date("2018-06-03"), temp: "25.4C" },
] )

The following aggregation operation on the weather collection parses the temp value and converts to a double:weather集合上的以下聚合操作解析temp值并转换为double值:

// Define stage to add degrees field with converted value
tempConversionStage = {
   $addFields: {
      degrees: { $toDouble: { $substrBytes: [ "$temp", 0, 4 ] } }
   }
};
db.weather.aggregate( [
   tempConversionStage,
] )

The operation returns the following documents:该操作返回以下文档:

{ "_id" : 1, "date" : ISODate("2018-06-01T00:00:00Z"), "temp" : "26.1C", "degrees" : 26.1 }
{ "_id" : 2, "date" : ISODate("2018-06-02T00:00:00Z"), "temp" : "25.1C", "degrees" : 25.1 }
{ "_id" : 3, "date" : ISODate("2018-06-03T00:00:00Z"), "temp" : "25.4C", "degrees" : 25.4 }
Note注意

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

←  $toDecimal (aggregation)$toInt (aggregation) →