0
0
MongoDBquery~20 mins

Type conversion expressions ($toInt, $toString) in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Type Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this MongoDB aggregation stage?
Consider the following aggregation pipeline stage applied to a collection with documents like { "value": "123" }:

{ "$project": { "intValue": { "$toInt": "$value" } } }

What will be the value of intValue in the output document?
MongoDB
{ "$project": { "intValue": { "$toInt": "$value" } } }
A{ "intValue": null }
B{ "intValue": "123" }
C{ "intValue": 123 }
DError: Cannot convert string to int
Attempts:
2 left
💡 Hint
The $toInt operator converts a string that looks like a number into an integer.
query_result
intermediate
2:00remaining
What happens when $toInt converts a floating-point number?
Given a document { "score": 98.7 }, what is the result of this aggregation stage?

{ "$project": { "intScore": { "$toInt": "$score" } } }
MongoDB
{ "$project": { "intScore": { "$toInt": "$score" } } }
AError: Cannot convert float to int
B{ "intScore": 98 }
C{ "intScore": 98.7 }
D{ "intScore": 99 }
Attempts:
2 left
💡 Hint
$toInt truncates the decimal part when converting floats.
📝 Syntax
advanced
2:00remaining
Which aggregation expression correctly converts a field to string?
You want to convert the field count to a string in an aggregation pipeline. Which of the following expressions is syntactically correct and will work?
A{ "$toString": count }
B{ "$toStr": "$count" }
C{ "$convert": { "input": "$count", "to": "string" } }
D{ "$toString": "$count" }
Attempts:
2 left
💡 Hint
The operator name is $toString and the field must be referenced as a string with $.
🔧 Debug
advanced
2:00remaining
Why does this aggregation stage cause an error?
Given documents with field price as strings like "12.99", why does this pipeline stage fail?

{ "$project": { "priceInt": { "$toInt": "$price" } } }
MongoDB
{ "$project": { "priceInt": { "$toInt": "$price" } } }
ABecause $toInt cannot convert strings with decimal points
BBecause $toInt requires numeric input, not strings
CBecause the field name must be without quotes
DBecause $toInt only works on integers, not floats
Attempts:
2 left
💡 Hint
Check how $toInt handles strings with decimal points.
🧠 Conceptual
expert
2:00remaining
Which statement about $toInt and $toString is true?
Select the correct statement about MongoDB's $toInt and $toString operators in aggregation pipelines.
A$toInt throws an error if the input cannot be converted exactly to an integer
B$toString only works on numeric types, not on arrays or objects
C$toString converts any BSON type to its string representation without error
D$toInt can convert strings representing integers and floats by truncating decimals
Attempts:
2 left
💡 Hint
Think about error handling and input types for these operators.