0
0
MongoDBquery~20 mins

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

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