$toInt
The `$toInt` operator converts a specified value into an integer value.
Syntax
{
$toInt: <expression>
}
Parameters
expression
objectrequiredThe specified value to convert into an Integer value.
Examples
Sample Data
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop",
"location": { "lat": -89.2384 }
}
Convert a Double to Integer
Convert the latitude field from double to int.
This query truncates a double value to an integer.
Query:
db.stores.aggregate([{
$match: { _id: "b0107631-9370-4acd-aafa-8ac3511e623d" }
}, {
$project: {
originalLatitude: "$location.lat",
latitudeAsInt: { $toInt: "$location.lat" }
}
}])
Output:
[
{
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
"originalLatitude": 72.8377,
"latitudeAsInt": 72
}
]
Convert a String to Integer
Convert the string "72" to an integer value.
This query converts a string representation of an integer.
Query:
db.stores.aggregate([{
$match: { _id: "b0107631-9370-4acd-aafa-8ac3511e623d" }
}, {
$project: {
originalLatitude: "$location.lat",
latitudeAsInt: {
$toInt: { $toString: "72" }
}
}
}])
Output:
[
{
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
"originalLatitude": 72.8377,
"latitudeAsInt": 72
}
]