String expressions ($concat, $toUpper, $toLower) in MongoDB - Time & Space Complexity
We want to understand how the time it takes to run string expressions changes as the input data grows.
How does using $concat, $toUpper, and $toLower affect the work done when processing many documents?
Analyze the time complexity of the following code snippet.
db.users.aggregate([
{
$project: {
fullName: { $concat: ["$firstName", " ", "$lastName"] },
upperName: { $toUpper: "$firstName" },
lowerName: { $toLower: "$lastName" }
}
}
])
This code creates new fields by joining first and last names and changing their case for each user document.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Processing each document once to apply string operations.
- How many times: Once per document in the collection.
As the number of documents grows, the total work grows proportionally because each document is processed separately.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 string operation sets |
| 100 | 100 string operation sets |
| 1000 | 1000 string operation sets |
Pattern observation: The work grows linearly with the number of documents.
Time Complexity: O(n)
This means the time to run these string expressions grows directly with the number of documents processed.
[X] Wrong: "String operations like $toUpper or $concat take the same time no matter how many documents there are."
[OK] Correct: Each document must be processed individually, so more documents mean more total work.
Understanding how string operations scale helps you explain performance when working with text data in databases.
"What if we added a nested array and used $map to apply $toUpper on each element? How would the time complexity change?"
Practice
{$toUpper: "hello"} do?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 CQuick Check:
$toUpper("hello") = "HELLO" [OK]
- Confusing $toUpper with $toLower
- Thinking $toUpper joins strings
- Assuming it removes spaces
firstName and lastName in MongoDB aggregation?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 BQuick Check:
$concat needs array of strings [OK]
- Using + operator inside $concat
- Passing arguments without array brackets
- Putting fields inside a single string
{ $concat: [ { $toUpper: "$name" }, " ", { $toLower: "$surname" } ] }Solution
Step 1: Apply $toUpper to "name" field
The "name" field is "alice", $toUpper converts it to "ALICE".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".Final Answer:
"ALICE smith" -> Option DQuick Check:
$toUpper("alice") + " " + $toLower("smith") = "ALICE smith" [OK]
- Assuming $toLower changes uppercase to title case
- Mixing order of operations
- Forgetting to use $concat array
{ $concat: "$firstName", " ", "$lastName" }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 AQuick Check:
$concat([...]) needs array brackets [OK]
- Passing multiple arguments without array
- Confusing $concat with string concatenation operator
- Not prefixing field names with $
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?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 AQuick Check:
Uppercase each name then join with space [OK]
- Uppercasing after concatenation changes space too
- Not applying $toUpper to both fields
- Using $toLower mistakenly
Ready to prove it?
Pass the test and this pattern moves unknown
