Challenge - 5 Problems
MongoDB Type Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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" }:
What will be the value of
{ "$project": { "intValue": { "$toInt": "$value" } } }What will be the value of
intValue in the output document?MongoDB
{ "$project": { "intValue": { "$toInt": "$value" } } }Attempts:
2 left
💡 Hint
The $toInt operator converts a string that looks like a number into an integer.
✗ Incorrect
The $toInt operator converts the string "123" into the integer 123. So the output document will have intValue as a number, not a string.
❓ query_result
intermediate2: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" } } }Attempts:
2 left
💡 Hint
$toInt truncates the decimal part when converting floats.
✗ Incorrect
The $toInt operator truncates the decimal part of a floating-point number, so 98.7 becomes 98.
📝 Syntax
advanced2: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?Attempts:
2 left
💡 Hint
The operator name is $toString and the field must be referenced as a string with $.
✗ Incorrect
Option D uses the correct operator $toString and references the field with "$count". Option D uses a non-existent operator. Option D is valid syntax but uses $convert, which is different from $toString. Option D misses quotes around the field reference.
🔧 Debug
advanced2: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" } } }Attempts:
2 left
💡 Hint
Check how $toInt handles strings with decimal points.
✗ Incorrect
$toInt cannot convert strings that represent decimal numbers like "12.99" directly, causing an error.
🧠 Conceptual
expert2:00remaining
Which statement about $toInt and $toString is true?
Select the correct statement about MongoDB's $toInt and $toString operators in aggregation pipelines.
Attempts:
2 left
💡 Hint
Think about error handling and input types for these operators.
✗ Incorrect
Option A is true: $toInt throws an error if the input cannot be converted exactly to an integer (e.g., strings with decimals). Option A is false because $toInt cannot convert floats represented as strings directly. Option A is false because $toString may fail on some BSON types. Option A is false because $toString can convert many BSON types including arrays and objects.