$toDecimal
The `$toDecimal` operator converts an input expression into a Decimal value. Long, Double or Int values are simply converted to a Decimal data type, while Decimal values are returned as is. A boolean value of true is converted to 1, while a boolean false is converted to 0. Lastly, ISODates are converted to a Decimal value corresponding to the number of milliseconds since January 1st, 1970 represented by the ISODate value.
Syntax
{
$toDecimal: <expression>
}
Parameters
expression
objectrequiredThe specified value to convert into a Decimal value.
Examples
Sample Data
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop",
"location": { "lat": -89.2384 }
}
Convert a Double to Decimal
Convert latitude field from double to decimal value.
This query converts a double to a Decimal128 value.
Query:
db.stores.aggregate([{
$match: { _id: "b0107631-9370-4acd-aafa-8ac3511e623d" }
}, {
$project: {
originalLatitude: "$location.lat",
latitudeAsDecimal: { $toDecimal: "$location.lat" }
}
}])
Output:
[
{
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
"originalLatitude": 72.8377,
"latitudeAsDecimal": "Decimal128('72.8377000000000')"
}
]
Convert an ISODate to Decimal
Convert an ISO date to decimal milliseconds.
This query converts a date to milliseconds since epoch.
Query:
db.stores.aggregate([{
$match: { _id: "b0107631-9370-4acd-aafa-8ac3511e623d" }
}, {
$project: {
dateAsDecimal: {
$toDecimal: ISODate("2025-01-06T00:00:00.000Z")
}
}
}])
Output:
[
{
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
"dateAsDecimal": "Decimal128('1736121600000')"
}
]