Though $toInt
is really useful, it was added on mongoDB 4.0, I've run into this same situation in a database running 3.2 which upgrading to use $toInt
was not an option due to some other application incompatibilities, so i had to come up with something else, and actually was surprisingly simple.
If you $project
and $add
zero to your string, it will turn into a number
{
$project : {
'convertedField' : { $add : ["$stringField",0] },
//more fields here...
}
}