The $toDouble operator converts values to a double (64-bit floating-point number) in MongoDB aggregation pipelines. Use it to parse numeric strings and enable arithmetic on imported or mixed-type data.
01
Double Conversion
Values to float.
02
Syntax
One expression.
03
String Parsing
Text to number.
04
$project Stage
Enable math ops.
05
Use Cases
Scores, averages.
06
vs $toDecimal
Float vs exact.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $toDouble operator converts an expression to a 64-bit floating-point number (double). Imported CSV and JSON data often stores numbers as strings like "85.5" or "3.14". $toDouble parses those strings so you can use $multiply, $avg, $sum, and other numeric operators.
$toDouble is shorthand for { $convert: { input: <expression>, to: "double" } }. It is part of the type-conversion family alongside $toInt, $toDecimal, and $toString.
💡
Beginner Tip
Use $toDouble for general numeric work like scores and measurements. For currency and financial calculations where every cent must be exact, prefer $toDecimal instead.
$toDouble errors on invalid strings. $convert with onError lets the pipeline continue with a safe default.
Applications
🚀 Use Cases
CSV/JSON import cleanup — parse numeric strings into doubles for math and sorting.
Statistical analysis — compute $avg, $stdDevPop, and other numeric aggregations on string fields.
Sensor and measurement data — convert text readings to numbers for charts and thresholds.
Rating systems — parse string ratings for averaging and ranking.
Schema migration — transform legacy string-number fields in aggregation pipelines.
🧠 How $toDouble Works
1
MongoDB reads the expression
The input resolves from a field path, numeric string, number, boolean, or nested expression.
Input
2
Value is converted to double
Strings are parsed as numbers; integers, longs, and decimals are cast to 64-bit float.
Convert
3
A double result is returned
The output supports $add, $multiply, $avg, sorting, and comparisons.
Output
=
📊
Numeric field ready
Use in math, aggregations, sorting, and reporting pipelines.
Wrap Up
Conclusion
The $toDouble operator converts values to 64-bit floating-point numbers in MongoDB aggregation pipelines. It is the go-to choice for parsing numeric strings and enabling general math on mixed-type data.
For currency and financial calculations, use $toDecimal instead. For conversions with error handling, use $convert. Next in the series: $toInt.
Use $toDouble to parse numeric strings before math operations
Convert before $sort when fields are stored as strings
Use $convert with onError for messy imported data
Choose $toDecimal when penny-level precision matters
Test with integers, decimals, null, and invalid strings
❌ Don’t
Use $toDouble for currency when exact cents are required
Assume string fields auto-convert in $multiply or $avg
Sort string numbers without converting first
Forget that invalid strings error with bare $toDouble
Mix unconverted strings and numbers in the same calculation
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $toDouble
Use these points when converting values to double in MongoDB.
5
Core concepts
📊01
To Double
64-bit float.
Purpose
📝02
{ $toDouble: x }
One argument.
Syntax
🔢03
Parse strings
Text to number.
Input
🛠04
$avg / $multiply
Enable math.
Usage
⚠05
Not for money
Use $toDecimal.
Compare
❓ Frequently Asked Questions
$toDouble converts a value to a double (64-bit floating-point number) inside an aggregation pipeline. It is an aggregation expression operator used in stages like $project and $addFields.
The syntax is { $toDouble: <expression> }. The expression can be a field reference like "$score", a numeric string, or another numeric expression.
Use $toDouble for general numeric work — scores, measurements, averages — where tiny floating-point rounding differences are acceptable. Use $toDecimal for currency and financial calculations that require exact decimal precision.
$toDouble is shorthand for { $convert: { input: <expr>, to: "double" } }. Use $convert with onError when invalid values should return a fallback instead of failing the pipeline.
$toDouble returns null when the input is null. It does not throw an error for null input.