The $concat operator joins multiple string values into one string in MongoDB aggregation pipelines. Use it to build full names, formatted labels, addresses, and display-friendly output from separate fields.
01
Join Strings
Combine fields + literals.
02
Syntax
Array of expressions.
03
Separators
Add spaces, commas, etc.
04
$project Stage
Create display fields.
05
Use Cases
Names, reports, labels.
06
Null Safety
Guard with $ifNull.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $concat operator concatenates an array of string expressions into a single string. For example, { $concat: ["$firstName", " ", "$lastName"] } produces "John Doe" when firstName is John and lastName is Doe.
💡
Beginner Tip
Include literal separators like " " (space), ", " (comma-space), or "-" between field references. Without them, values are joined with no gap.
Foundation
📝 Syntax
The $concat operator takes an array of string expressions:
Wrap numeric fields with $toString before passing them to $concat. For formatted currency, consider $round before converting.
Applications
🚀 Use Cases
Data formatting — build composite display fields from separate string columns.
Report generation — create readable labels for exports and dashboards.
Full name assembly — join first, middle, and last name fields with separators.
Textual analysis — merge text fields before search or pattern matching downstream.
🧠 How $concat Works
1
MongoDB evaluates each expression
Field references and literals in the array are resolved to string values (or null).
Input
2
$concat joins them in order
Values are appended left to right with no automatic separator. If any value is null, the result is null.
Join
3
The combined string is stored
The result is written to the field you define in $project or $addFields.
Output
=
🔗
Single combined string
Ready for display, export, or further string operations.
Wrap Up
Conclusion
The $concat operator is an essential string tool in MongoDB aggregation pipelines. It lets you merge fields and literals into a single readable value — perfect for names, addresses, order labels, and report formatting.
For beginners, remember: include separators explicitly, guard null fields with $ifNull, and use $toString for numbers. For joining arrays, use $concatArrays instead.
Keep separator literals consistent across pipelines
Use $trim after concat if spaces may trail
❌ Don’t
Confuse $concat with $concatArrays
Pass numbers directly without $toString
Forget that one null field nullifies the whole result
Assume MongoDB adds spaces between fields automatically
Concatenate very large strings without considering document size
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $concat
Use these points when joining strings in MongoDB.
5
Core concepts
🔗01
Join Strings
Array of expressions.
Purpose
📝02
Array Syntax
{ $concat: [a, b] }
Syntax
📏03
Add Separators
Literals like " " or ", ".
Tip
⚠04
Null = Null
Use $ifNull to guard.
Edge case
🔢05
Numbers
Convert with $toString.
Types
❓ Frequently Asked Questions
$concat combines multiple string values into a single string. It accepts an array of expressions — field references, literals, or other string expressions.
The syntax is { $concat: [ <expression1>, <expression2>, ... ] }. You can include as many expressions as needed, including literal separators like a space or comma.
If any expression evaluates to null, $concat returns null for the entire result. Use $ifNull on individual fields to provide fallback empty strings.
$concat joins strings into one string. $concatArrays joins multiple arrays into one array. They operate on different data types.
$concat only works with strings. Convert numbers first using $toString, then pass the result to $concat.