Definition
$toDoubleConverts a value to a double. If the value cannot be converted to an double,
$toDoubleerrors. If the value is null or missing,$toDoublereturns null.$toDoublehas the following syntax:{ $toDouble: <expression> } The
$toDoubletakes any valid expression.The
$toDoubleis a shorthand for the following$convertexpression:{ $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.Returns 1 for true. |
Double | No-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. |
Integer | Returns the int value as a double. |
Long | Returns the long value as a 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.
You cannot convert a string value of a non-base 10
number (e.g. |
Date | Returns the number of milliseconds since the epoch that corresponds to the date value. |
Tip
To convert strings in bases other than 10, use
$convert with the base option.
The following table lists some conversion to double examples:
Example | Results |
|---|---|
| 1 |
| 0 |
| 2.5 |
| 5 |
| 10000 |
| -5.5 |
| 1522127087890 |
Subnormal Numbers
Starting in MongoDB 8.3, the server is able to parse the full range of all representable double precision floating point numbers. This includes subnormal numbers where the most significant digit has leading zeroes and the exponent has the least possible value. For details, see Subnormal Number.
In earlier versions of MongoDB, the server returns an error when you try to parse these numbers. The following example raises an error in versions earlier than MongoDB 8.3:
db.t.insertOne( { v: "7.08263e-317" } ) db.t.aggregate([ { $project: { converted: { $convert: { input: "$v", to: "double" }, } } } ])
This example fails with an error similar to the following:
MongoServerError[ConversionFailure]: Executor error during aggregate command on namespace: test.t :: caused by :: Failed to parse number '7.08263e-317' in $convert with no onError value: Out of range
Example
Create a collection weather with the following documents:
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:
// 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.