On this page本页内容
$toDouble
¶New in version 4.0.版本4.0中的新功能。
Converts a value to a double. 将值转换为double。If the value cannot be converted to an double, 如果该值无法转换为double,$toDouble
errors. $toDouble
将产生错误。If the value is null or missing, 如果值为$toDouble
returns null.null
或缺失,$toDouble
返回null
。
$toDouble
has the following syntax:语法如下所示:
The $toDouble
takes any valid expression.$toDouble
接受任何有效的表达式。
The $toDouble
is a shorthand for the following $convert
expression:
The following table lists the input types that can be converted to a double:下表列出了可以转换为double的输入类型:
Input Type | Behavior |
---|---|
Boolean | Returns
0 for false .Returns 1 for true . |
Double | |
Decimal |
|
Integer | |
Long | |
String |
The string value must be of a base10 numeric value (e.g. You cannot convert a string value of a non-base10 number (e.g. |
Date |
The following table lists some conversion to double examples:下表列出了一些转换为双重示例:
$toDouble: true |
1 |
$toDouble: false |
0 |
$toDouble: 2.5 |
2.5 |
$toDouble: NumberInt(5) |
5 |
$toDouble: NumberLong(10000) |
10000 |
$toDouble: "-5.5" |
-5.5 |
$toDouble: ISODate("2018-03-27T05:04:47.890Z") |
1522127087890 |
Create a collection 使用以下文档创建集合weather
with the following documents:weather
:
The following aggregation operation on the weather
collection parses the temp
value and converts to a double:weather
集合上的以下聚合操作将解析temp
值并转换为double:
The operation returns the following documents:该操作返回以下文档: