Docs HomeMongoDB Manual

$toDouble(aggregation)

On this page本页内容

Definition定义

$toDouble

Converts a value to a double. 将值转换为双精度。If 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行为
BooleanReturns 0 for false.如果为false,则返回0
Returns 1 for true.如果为true,则返回1
DoubleNo-op. Returns the double.没有。返回双精度。
DecimalReturns 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.以双精度形式返回int值。
LongReturns the long value as a double.以双精度形式返回长值。
StringReturns 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. 不能将非基数为10的字符串值(例如“0x6400”)或超出最小值和最大值的值转换为双精度。
DateReturns the number of milliseconds since the epoch that corresponds to the date value.返回自与日期值对应的epoch以来的毫秒数。

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