📖 Scenario: You work in a small bookstore's database team. The store keeps records of books with separate fields for the author's first name and last name. You want to create a new field that shows the author's full name in uppercase letters for better display on the website.
🎯 Goal: Build a MongoDB aggregation pipeline that combines the author's first and last names into a single full name string, converts it to uppercase, and stores it in a new field called authorFullName.
📋 What You'll Learn
Create a collection named books with documents containing authorFirstName and authorLastName fields.
Add a configuration variable space that holds a single space character.
Use the $concat expression to join the first name, space, and last name into one string.
Use the $toUpper expression to convert the concatenated full name to uppercase.
Add the resulting uppercase full name as a new field authorFullName in the aggregation output.
💡 Why This Matters
🌍 Real World
Bookstores and libraries often store author names in separate fields. Combining and formatting these names helps display them nicely on websites and reports.
💼 Career
Database developers and data engineers frequently use string expressions in MongoDB aggregation pipelines to transform and prepare data for applications.
Progress0 / 4 steps
1
Create the books collection with author names
Create a MongoDB collection called books with these exact documents: { authorFirstName: "Jane", authorLastName: "Austen" } and { authorFirstName: "Mark", authorLastName: "Twain" }.
MongoDB
Hint
Use insertMany to add multiple documents to the books collection.
2
Add a space variable for joining names
Create a variable called space and set it to a string containing a single space character " ".
MongoDB
Hint
Use const space = " " to store a space character.
3
Use $concat to join first and last names with space
Write a MongoDB aggregation pipeline stage that uses $concat to join authorFirstName, the variable space, and authorLastName into a new field called fullName.
MongoDB
Hint
Use $addFields with $concat to join the fields and the space variable.
4
Convert the full name to uppercase with $toUpper
Extend the aggregation pipeline to add a new field called authorFullName that converts the fullName field to uppercase using $toUpper.
MongoDB
Hint
Use $toUpper around the fullName field to create authorFullName.
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
Step 1: Understand $toUpper usage
The $toUpper operator converts all letters in a string to uppercase.
Step 2: Apply $toUpper to "hello" string
Applying $toUpper to "hello" changes it to "HELLO".
Final Answer:
Converts the string "hello" to uppercase "HELLO" -> Option C
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
Step 1: Recall $concat syntax
$concat requires an array of strings or field references to join them.
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.
Final Answer:
{$concat: ["$firstName", " ", "$lastName"]} -> Option B
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?
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
Step 1: Check $concat argument format
$concat requires a single array argument containing strings or field references.
Step 2: Identify missing array brackets
The expression passes multiple arguments without wrapping them in an array, causing syntax error.
Final Answer:
Missing array brackets around the arguments for $concat -> Option A
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
Step 1: Understand requirement for uppercase names
Both firstName and lastName must be converted to uppercase individually before concatenation.
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.
Final Answer:
{$concat: [{$toUpper: "$firstName"}, " ", {$toUpper: "$lastName"}]} -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.