Bird
Raised Fist0
MongoDBquery~20 mins

String expressions ($concat, $toUpper, $toLower) in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Expression 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 pipeline?
Consider the following document in a collection:
{ "firstName": "Alice", "lastName": "Smith" }
What will be the value of the field fullName after running this pipeline?
[{ $project: { fullName: { $concat: ["$firstName", " ", { $toUpper: "$lastName" }] } } }]
MongoDB
[{ $project: { fullName: { $concat: ["$firstName", " ", { $toUpper: "$lastName" }] } } }]
A"Alice SMITH"
B"ALICE Smith"
C"alice smith"
D"Alice Smith"
Attempts:
2 left
💡 Hint
Remember that $toUpper converts the string to uppercase letters.
❓ query_result
intermediate
2:00remaining
What does this aggregation pipeline output for the field greeting?
Given a document:
{ "name": "bob" }
Run this pipeline:
[{ $project: { greeting: { $concat: ["Hello, ", { $toUpper: { $substrCP: ["$name", 0, 1] } }, { $toLower: { $substrCP: ["$name", 1, 2] } }] } } }]
MongoDB
[{ $project: { greeting: { $concat: ["Hello, ", { $toUpper: { $substrCP: ["$name", 0, 1] } }, { $toLower: { $substrCP: ["$name", 1, 2] } }] } } }]
A"Hello, Bob"
B"Hello, BOB"
C"Hello, bob"
D"Hello, BoB"
Attempts:
2 left
💡 Hint
Look at how the first letter is uppercased and the rest lowercased.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this MongoDB aggregation expression?
Identify the option that will cause a syntax error when used inside a $project stage:
{ fullName: { $concat: ["$firstName", " ", $toUpper: "$lastName" ] }
MongoDB
{ fullName: { $concat: ["$firstName", " ", $toUpper: "$lastName" ] } }
A{ fullName: { $concat: ["$firstName", " ", { $toUpper: "$lastName" } ] } }
B{ fullName: { $concat: ["$firstName", " ", { $toUpper: "$lastName" }] } }
C{ fullName: { $concat: ["$firstName", " ", $toUpper: "$lastName" ] } }
D{ fullName: { $concat: ["$firstName", " ", { $toUpper: ["$lastName"] }] } }
Attempts:
2 left
💡 Hint
Check how $toUpper is wrapped inside the $concat array.
❓ optimization
advanced
2:00remaining
Which option is the most efficient way to concatenate first and last names in uppercase?
You want to create a field fullNameUpper that combines firstName and lastName in uppercase letters.
Which pipeline stage is best?
A{ $project: { fullNameUpper: { $toUpper: "$firstName" } } }
B{ $project: { fullNameUpper: { $toUpper: { $concat: ["$firstName", " ", "$lastName"] } } } }
C{ $project: { fullNameUpper: { $concat: ["$firstName", " ", "$lastName"] } } }
D{ $project: { fullNameUpper: { $concat: [ { $toUpper: "$firstName" }, " ", { $toUpper: "$lastName" } ] } } }
Attempts:
2 left
💡 Hint
Applying $toUpper once after concatenation is usually more efficient.
🧠 Conceptual
expert
3:00remaining
What will be the output of this aggregation pipeline for the field result?
Given a document:
{ "word": "MongoDB" }
Run this pipeline:
[{ $project: { result: { $concat: [ { $toLower: { $substrCP: ["$word", 0, 3] } }, { $toUpper: { $substrCP: ["$word", 3, 3] } }, { $toLower: { $substrCP: ["$word", 6, 2] } } ] } } }]
MongoDB
[{ $project: { result: { $concat: [ { $toLower: { $substrCP: ["$word", 0, 3] } }, { $toUpper: { $substrCP: ["$word", 3, 3] } }, { $toLower: { $substrCP: ["$word", 6, 2] } } ] } } }]
A"monGODB"
B"mongodb"
C"monGODBd"
D"monGODb"
Attempts:
2 left
💡 Hint
Look carefully at the substring positions and lengths.

Practice

(1/5)
1. What does the MongoDB expression {$toUpper: "hello"} do?
easy
A. Joins the string "hello" with another string
B. Converts the string "hello" to lowercase "hello"
C. Converts the string "hello" to uppercase "HELLO"
D. Removes spaces from the string "hello"

Solution

  1. Step 1: Understand $toUpper usage

    The $toUpper operator converts all letters in a string to uppercase.
  2. Step 2: Apply $toUpper to "hello" string

    Applying $toUpper to "hello" changes it to "HELLO".
  3. Final Answer:

    Converts the string "hello" to uppercase "HELLO" -> Option C
  4. Quick Check:

    $toUpper("hello") = "HELLO" [OK]
Hint: Remember $toUpper makes all letters uppercase [OK]
Common Mistakes:
  • Confusing $toUpper with $toLower
  • Thinking $toUpper joins strings
  • Assuming it removes spaces
2. Which of the following is the correct syntax to concatenate two fields firstName and lastName in MongoDB aggregation?
easy
A. {$concat: "$firstName" + " " + "$lastName"}
B. {$concat: ["$firstName", " ", "$lastName"]}
C. {$concat: "$firstName", " ", "$lastName"}
D. {$concat: "$firstName $lastName"}

Solution

  1. Step 1: Recall $concat syntax

    $concat requires an array of strings or field references to join them.
  2. Step 2: Check each option's syntax

    {$concat: ["$firstName", " ", "$lastName"]} correctly uses an array with field references and a space string. Others misuse concatenation or array syntax.
  3. Final Answer:

    {$concat: ["$firstName", " ", "$lastName"]} -> Option B
  4. Quick Check:

    $concat needs array of strings [OK]
Hint: Use array syntax with $concat for multiple strings [OK]
Common Mistakes:
  • Using + operator inside $concat
  • Passing arguments without array brackets
  • Putting fields inside a single string
3. Given the document {"name": "alice", "surname": "smith"}, what is the output of this aggregation expression?
{ $concat: [ { $toUpper: "$name" }, " ", { $toLower: "$surname" } ] }
medium
A. "Alice Smith"
B. "alice smith"
C. "ALICE SMITH"
D. "ALICE smith"

Solution

  1. Step 1: Apply $toUpper to "name" field

    The "name" field is "alice", $toUpper converts it to "ALICE".
  2. Step 2: Apply $toLower to "surname" field and concatenate

    The "surname" field is "smith", $toLower keeps it "smith". Then $concat joins "ALICE", space, and "smith".
  3. Final Answer:

    "ALICE smith" -> Option D
  4. Quick Check:

    $toUpper("alice") + " " + $toLower("smith") = "ALICE smith" [OK]
Hint: Apply $toUpper and $toLower before concatenation [OK]
Common Mistakes:
  • Assuming $toLower changes uppercase to title case
  • Mixing order of operations
  • Forgetting to use $concat array
4. Identify the error in this MongoDB aggregation expression:
{ $concat: "$firstName", " ", "$lastName" }
medium
A. Missing array brackets around the arguments for $concat
B. Using double quotes instead of single quotes
C. Incorrect field names without $ prefix
D. Using $concat instead of $toUpper

Solution

  1. Step 1: Check $concat argument format

    $concat requires a single array argument containing strings or field references.
  2. Step 2: Identify missing array brackets

    The expression passes multiple arguments without wrapping them in an array, causing syntax error.
  3. Final Answer:

    Missing array brackets around the arguments for $concat -> Option A
  4. Quick Check:

    $concat([...]) needs array brackets [OK]
Hint: Always wrap $concat inputs in an array [OK]
Common Mistakes:
  • Passing multiple arguments without array
  • Confusing $concat with string concatenation operator
  • Not prefixing field names with $
5. You want to create a new field fullName in your aggregation pipeline that combines firstName and lastName with a space, but both names should be in uppercase. Which expression correctly achieves this?
hard
A. {$concat: [{$toUpper: "$firstName"}, " ", {$toUpper: "$lastName"}]}
B. {$toLower: {$concat: ["$firstName", " ", "$lastName"]}}
C. {$concat: ["$firstName", " ", "$lastName"]}
D. {$toLower: {$concat: [{$toUpper: "$firstName"}, " ", {$toUpper: "$lastName"}]}}

Solution

  1. Step 1: Understand requirement for uppercase names

    Both firstName and lastName must be converted to uppercase individually before concatenation.
  2. Step 2: Analyze each option's logic

    {$concat: [{$toUpper: "$firstName"}, " ", {$toUpper: "$lastName"}]} applies $toUpper to each field separately, then concatenates with space. {$toLower: {$concat: ["$firstName", " ", "$lastName"]}} concatenates first, then lowercases whole string, resulting in lowercase. {$concat: ["$firstName", " ", "$lastName"]} does no uppercase. {$toLower: {$concat: [{$toUpper: "$firstName"}, " ", {$toUpper: "$lastName"}]}} converts final string to lowercase, which is wrong.
  3. Final Answer:

    {$concat: [{$toUpper: "$firstName"}, " ", {$toUpper: "$lastName"}]} -> Option A
  4. Quick Check:

    Uppercase each name then join with space [OK]
Hint: Uppercase each name before concat for correct result [OK]
Common Mistakes:
  • Uppercasing after concatenation changes space too
  • Not applying $toUpper to both fields
  • Using $toLower mistakenly
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