0
0
MongoDBquery~10 mins

Type conversion expressions ($toInt, $toString) in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type conversion expressions ($toInt, $toString)
Start with input value
Apply $toInt or $toString
Check if conversion is possible
Return converted
Output
The flow shows how MongoDB converts a value to integer or string using $toInt or $toString, checking if conversion is possible and returning the converted value or error.
Execution Sample
MongoDB
[
  { $project: { numberAsInt: { $toInt: "$numberStr" }, numberAsStr: { $toString: "$number" } } }
]
This aggregation stage converts a string field 'numberStr' to integer and an integer field 'number' to string.
Execution Table
StepInput ValueExpressionConversion ResultNotes
1"123"$toInt123String '123' converts to integer 123
2123$toString"123"Integer 123 converts to string '123'
3"abc"$toIntError or nullString 'abc' cannot convert to integer
4true$toString"true"Boolean true converts to string 'true'
5null$toIntnullNull converts to null for $toInt
645.67$toInt45Float 45.67 converts to integer 45 by truncation
7"45.67"$toIntError or nullString '45.67' cannot convert directly to int
8Exit--No more conversions
💡 Execution stops after all input values are processed or conversion fails.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
inputValue"123"123"abc"truenull45.67"45.67"ExitExit
convertedValueN/A123"123"Error/null"true"null45Error/nullEnd
Key Moments - 3 Insights
Why does converting the string '45.67' with $toInt fail while the float 45.67 converts successfully?
Because $toInt can convert numeric types like float by truncation, but cannot convert strings with decimal points directly, causing an error or null (see execution_table row 6 and 7).
What happens when $toInt tries to convert a non-numeric string like 'abc'?
It results in an error or null because the string does not represent a valid number (see execution_table row 3).
How does $toString handle boolean values?
$toString converts boolean true or false to their string equivalents 'true' or 'false' (see execution_table row 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the conversion result of input value "123" with $toInt?
A123
B"123"
CError or null
Dnull
💡 Hint
Check execution_table row 1 under Conversion Result.
At which step does $toInt conversion fail due to invalid string input?
AStep 2
BStep 3
CStep 6
DStep 4
💡 Hint
Look for 'Error or null' in Conversion Result column in execution_table.
If the input value is boolean true, what does $toString convert it to?
A"true"
Btrue
C1
DError
💡 Hint
See execution_table row 4 Conversion Result.
Concept Snapshot
$toInt and $toString convert values to integer or string.
Use $toInt to convert strings or numbers to integers (floats truncate).
Use $toString to convert any value to string.
Invalid conversions return error or null.
Useful in aggregation pipelines for type consistency.
Full Transcript
This visual execution trace shows how MongoDB's $toInt and $toString expressions convert values. Starting with an input value, the expression attempts conversion. If successful, the converted value is returned; otherwise, an error or null is returned. Examples include converting string '123' to integer 123, integer 123 to string '123', and handling invalid conversions like string 'abc' to integer which fails. The trace highlights how floats convert by truncation and booleans convert to their string forms. This helps beginners understand step-by-step how type conversions behave in MongoDB aggregation pipelines.