0
0
MongoDBquery~10 mins

String expressions ($concat, $toUpper, $toLower) in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String expressions ($concat, $toUpper, $toLower)
Start with input strings
Apply $concat to join strings
Apply $toUpper or $toLower to change case
Output transformed string
This flow shows how MongoDB string expressions combine and change case of strings step-by-step.
Execution Sample
MongoDB
db.collection.aggregate([
  { $addFields: {
    fullName: { $concat: ["John", " ", "Doe"] }
  } },
  { $addFields: {
    shoutName: { $toUpper: "$fullName" }
  } },
  { $addFields: {
    whisperName: { $toLower: "$fullName" }
  } }
])
This aggregation creates a full name by joining strings, then makes uppercase and lowercase versions.
Execution Table
StepExpressionInput ValuesOperationResult
1$concat["John", " ", "Doe"]Join strings with space"John Doe"
2$toUpper"John Doe"Convert all letters to uppercase"JOHN DOE"
3$toLower"John Doe"Convert all letters to lowercase"john doe"
4End-All expressions evaluatedOutput fields: fullName, shoutName, whisperName
💡 All string expressions processed, final transformed strings ready.
Variable Tracker
VariableStartAfter $concatAfter $toUpperAfter $toLower
fullNameundefined"John Doe""John Doe""John Doe"
shoutNameundefinedundefined"JOHN DOE""JOHN DOE"
whisperNameundefinedundefinedundefined"john doe"
Key Moments - 2 Insights
Why does $toUpper and $toLower use the fullName field as input?
Because $concat creates the fullName string first (see execution_table step 1), then $toUpper and $toLower transform that result (steps 2 and 3).
Can $concat join non-string values directly?
No, $concat expects strings. Non-string values must be converted to strings first, otherwise it causes an error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of $concat at step 1?
A"JohnDoe"
B"John Doe"
C"John Doe"
D"john doe"
💡 Hint
Check the 'Result' column in execution_table row for step 1.
At which step does the string become all uppercase?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Operation' and 'Result' columns in execution_table for step 2.
If we swapped $toUpper and $toLower order, what would change in variable_tracker?
ANo change at all
BfullName would change
CshoutName and whisperName values would swap after step 3 and 4
DThe $concat result would be different
💡 Hint
Check variable_tracker columns 'After $toUpper' and 'After $toLower' for shoutName and whisperName.
Concept Snapshot
$concat: joins multiple strings into one
$toUpper: converts string to all uppercase letters
$toLower: converts string to all lowercase letters
Use in $project or $addFields to transform string fields
Order matters: apply $concat before case changes
Full Transcript
This visual execution trace shows how MongoDB string expressions $concat, $toUpper, and $toLower work step-by-step. First, $concat joins strings like "John", " ", and "Doe" into "John Doe". Then $toUpper converts "John Doe" to "JOHN DOE" and $toLower converts it to "john doe". Variables track these changes after each step. Key points include that $concat must run before case changes and that $concat only works with strings. The quiz tests understanding of each step's output and the effect of changing expression order.