The $toDecimal operator converts values to Decimal128 in MongoDB aggregation pipelines. Use it for financial data, currency calculations, and any scenario where floating-point rounding errors are unacceptable.
01
Decimal128
High-precision type.
02
Syntax
One expression.
03
Precision
No float rounding.
04
$project Stage
Parse price fields.
05
Use Cases
Finance, e-commerce.
06
vs $toDouble
Exact vs floating.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $toDecimal operator converts an expression to a BSON Decimal128 value. Standard doubles use binary floating point, which can produce surprising results — for example, 0.1 + 0.2 may not equal exactly 0.3. Decimal128 stores numbers as exact decimal values, making it the right choice for money, tax calculations, and accounting reports.
$toDecimal is shorthand for { $convert: { input: <expression>, to: "decimal" } }. It is part of the type-conversion family alongside $toInt, $toDouble, and $toString.
💡
Beginner Tip
Use $toDecimal when working with currency. For general scientific or statistical math where tiny rounding differences are acceptable, $toDouble is often sufficient and slightly faster.
Converting both operands to Decimal128 before $multiply ensures the result is exact. With doubles, small values like 0.10 can accumulate rounding errors over many calculations.
Example 3 — Sum Order Totals with $group
Aggregate precise decimal amounts across all orders:
$toDecimal errors on invalid strings. $convert with onError lets the pipeline continue with a safe default.
Applications
🚀 Use Cases
E-commerce pricing — store and calculate product prices, discounts, and totals with penny accuracy.
Accounting and invoicing — sum line items, apply tax rates, and generate financial reports.
Currency conversion — multiply amounts by exchange rates without floating-point drift.
Data import cleanup — parse string prices from CSV/JSON into proper decimal types.
Regulatory compliance — meet precision requirements for financial record keeping.
🧠 How $toDecimal Works
1
MongoDB reads the expression
The input resolves from a field path, numeric string, number, or nested expression.
Input
2
Value is parsed to Decimal128
Strings and numbers are converted to exact decimal representation with up to 34 significant digits.
Convert
3
Precise decimal is returned
The Decimal128 value supports exact $add, $multiply, $sum, and comparisons.
Output
=
💰
Exact financial math
Use for totals, tax, and reports where every cent must be correct.
Wrap Up
Conclusion
The $toDecimal operator converts values to Decimal128 for high-precision decimal arithmetic in MongoDB aggregation pipelines. It is the right choice for currency, tax, and any calculation where floating-point rounding errors are unacceptable.
For general numeric work where tiny rounding differences are fine, consider $toDouble instead. For conversions with error handling, use $convert. Next in the series: $toDouble.
Use $toDecimal for all currency and financial calculations
Convert both operands before $multiply or $add
Use $convert with onError for messy imported price data
Store prices as Decimal128 in new collections when precision matters
Test edge cases like 0.10, large totals, and zero values
❌ Don’t
Use $toDouble for money when penny accuracy is required
Mix Decimal128 and double in the same calculation without converting
Assume string prices will auto-convert in arithmetic expressions
Forget that invalid numeric strings error with bare $toDecimal
Overuse decimals for non-financial data where doubles are sufficient
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $toDecimal
Use these points when working with precise decimal values in MongoDB.
5
Core concepts
💰01
Decimal128
Exact decimals.
Purpose
📝02
{ $toDecimal: x }
One argument.
Syntax
🔢03
No float error
0.1 + 0.2 = 0.3.
Precision
🛠04
Money math
Prices, tax, sums.
Usage
⚠05
vs $toDouble
Exact vs float.
Compare
❓ Frequently Asked Questions
$toDecimal converts a value to Decimal128 inside an aggregation pipeline. Decimal128 provides high-precision decimal arithmetic, which is important for financial and currency calculations.
The syntax is { $toDecimal: <expression> }. The expression can be a field reference like "$price", a numeric string, or another numeric expression.
$toDouble uses binary floating point, which can introduce rounding errors (e.g. 0.1 + 0.2). $toDecimal uses Decimal128 for exact decimal representation, making it better for money and accounting.
$toDecimal is shorthand for { $convert: { input: <expr>, to: "decimal" } }. Use $convert with onError when invalid values should return a fallback instead of failing the pipeline.
$toDecimal returns null when the input is null. It does not throw an error for null input.