Bird
Raised Fist0
MongoDBquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the $toInt expression do in MongoDB aggregation?
easy
A. Converts a value to a date
B. Converts a value to an integer (whole number)
C. Converts a value to a boolean (true/false)
D. Converts a value to a string (text)

Solution

  1. Step 1: Understand the purpose of $toInt

    The $toInt operator converts values to integers, which are whole numbers without decimals.
  2. Step 2: Compare with other conversion types

    $toString converts to text, $toBool to boolean, and $toDate to date. So $toInt specifically converts to integer.
  3. Final Answer:

    Converts a value to an integer (whole number) -> Option B
  4. Quick Check:

    $toInt = integer conversion [OK]
Hint: Remember: Int means whole number, String means text [OK]
Common Mistakes:
  • Confusing $toInt with $toString
  • Thinking $toInt converts to decimal numbers
  • Mixing up $toInt with boolean conversion
2. Which of the following is the correct syntax to convert a field age to string in a MongoDB aggregation pipeline?
easy
A. { $toInt: "$age" }
B. { $toString: age }
C. { $toString: "$age" }
D. { toString: "$age" }

Solution

  1. Step 1: Identify correct operator and field reference

    The operator for string conversion is $toString. Field names must be referenced as strings with a $ prefix inside quotes, like "$age".
  2. Step 2: Check syntax correctness

    { $toString: "$age" } uses correct operator and field reference. { $toString: age } misses quotes around field. { $toInt: "$age" } uses wrong operator $toInt. { toString: "$age" } misses $ sign before operator.
  3. Final Answer:

    { $toString: "$age" } -> Option C
  4. Quick Check:

    Use $ and quotes for fields, $toString for string conversion [OK]
Hint: Use $ before field names and operators in MongoDB [OK]
Common Mistakes:
  • Omitting quotes around field names
  • Forgetting $ before operator
  • Using wrong operator for conversion
3. Given the document { "score": "85" }, what will be the result of this aggregation expression: { $toInt: "$score" }?
medium
A. 85 (as an integer)
B. "85" (as a string)
C. null
D. Error: Cannot convert string to int

Solution

  1. Step 1: Understand input and conversion

    The field "score" is a string "85". Using $toInt converts this string to the integer 85.
  2. Step 2: Confirm output type

    The output will be the number 85, not a string or error, because "85" is a valid numeric string.
  3. Final Answer:

    85 (as an integer) -> Option A
  4. Quick Check:

    String "85" to int = 85 [OK]
Hint: Numeric strings convert cleanly to integers [OK]
Common Mistakes:
  • Expecting output to remain a string
  • Thinking conversion causes error
  • Confusing null with error
4. You wrote this aggregation expression: { $toInt: 123abc }. What is the problem?
medium
A. It returns null silently
B. It converts to integer 123
C. It converts to string "123abc"
D. Syntax error because 123abc is not a valid number or string

Solution

  1. Step 1: Analyze the input value

    The value 123abc is not a valid number or quoted string. MongoDB expects a number or string literal for conversion.
  2. Step 2: Understand MongoDB parsing rules

    Without quotes, 123abc is invalid syntax, causing a syntax error.
  3. Final Answer:

    Syntax error because 123abc is not a valid number or string -> Option D
  4. Quick Check:

    Invalid literals cause syntax errors [OK]
Hint: Always quote strings; invalid literals cause errors [OK]
Common Mistakes:
  • Assuming partial conversion works
  • Forgetting to quote strings
  • Expecting silent null return
5. You have a collection with documents like { "value": 123 } and { "value": "456" }. You want to create a new field valueStr that is always a string. Which aggregation expression correctly converts value to string regardless of original type?
hard
A. { $toString: "$value" }
B. { $toInt: "$value" }
C. { $toString: value }
D. { $toString: { $toInt: "$value" } }

Solution

  1. Step 1: Understand the data types

    Some values are numbers (123), some are strings ("456"). We want to convert all to strings.
  2. Step 2: Choose correct conversion

    $toString converts any type to string. Using { $toString: "$value" } converts both numbers and strings to string type.
  3. Step 3: Evaluate other options

    { $toInt: "$value" } converts to int, not string. { $toString: value } misses quotes around field name. { $toString: { $toInt: "$value" } } converts to int then string, unnecessary and may cause errors if string is non-numeric.
  4. Final Answer:

    { $toString: "$value" } -> Option A
  5. Quick Check:

    Use $toString with quoted field for consistent string conversion [OK]
Hint: Use $toString with "$field" to convert any type to string [OK]
Common Mistakes:
  • Missing quotes around field name
  • Using $toInt instead of $toString
  • Unnecessary double conversion causing errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard