$toDecimal

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Conversion and type inspection

What it does

Converts compatible values to BSON Decimal128.

Syntax

{$toDecimal: expression}

Use case

Financial calculations needing decimal semantics.

$toDecimal: practical example
{ $set: { amount: { $toDecimal: "$amountText" } } }
💡 Explanation Converts compatible values to BSON Decimal128. When to use it Financial calculations needing decimal semantics.
Key Takeaways
  • Converts compatible values to BSON Decimal128.
  • Typical use: Financial calculations needing decimal semantics.