$toDouble
New from version 4.0
The $toDouble operator in Amazon DocumentDB is used to convert a value to a double-precision 64-bit floating-point number. This can be useful when you need to perform arithmetic operations on values that are not originally in a numeric format.
Parameters
<expression>: The expression to convert to a double value. This can be any valid expression that resolves to a numeric, string, or boolean value.
Example (MongoDB Shell)
This example demonstrates how to use the $toDouble operator to convert a string value to a numeric value for the purpose of performing a mathematical calculation.
Create sample documents
db.numbers.insertMany([ { _id: 1, value: "10.5" }, { _id: 2, value: "20.25" }, { _id: 3, value: "7" } ])
Query example
db.numbers.aggregate([ { $project: { _id: 1, value: 1, double_value: { $toDouble: "$value" }, double_plus_five: { $add: [{ $toDouble: "$value" }, 5] } } } ])
Output
[
{ "_id" : 1, "value" : "10.5", "double_value" : 10.5, "double_plus_five" : 15.5 },
{ "_id" : 2, "value" : "20.25", "double_value" : 20.25, "double_plus_five" : 25.25 },
{ "_id" : 3, "value" : "7", "double_value" : 7.0, "double_plus_five" : 12.0 }
]
Code examples
To view a code example for using the $toDouble command, choose the tab for the language that you want to use: