0
0
MongoDBquery~30 mins

String expressions ($concat, $toUpper, $toLower) in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Using String Expressions in MongoDB Aggregation
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use $toUpper around the fullName field to create authorFullName.