String to int v. 3.6

Is there an equivalent for $toInt in versions prior to 4.0

Hey @Gerard_51369

With aggregation along I do not believe so. You could query for the documents and then map over them in your application to convert the types you want.

I could be missing something if anyone else knows different.

I don’t know much about the older versions of MongoDB but if $toInt is new, I would imagine that you can perform the conversion using python in combination with a collection update.
I.e. query using $type to filter out the string field docs, then loop through the resulting docs in Python and perform the bulk update.

I’m surprised that such a basic operation wasn’t available from the beginning.

Thanks for the replies guys. Was hoping there was a simpler workaround in an aggregation.

Hi @Gerard_51369

For the purpose of comparison, you can also refer to Collation in MongoDB.
Specifically of integers, you can use numericOrdering field which determines whether to compare numeric strings as numbers or as strings.

Please let me know, if you have any questions.