$concat is a MongoDB aggregation expression that joins multiple strings into one. Pass an array of pieces—fields, literals, or other string expressions. If any piece is null or missing, the whole result is null.
Remember
$concat = join strings in aggregation
{ $concat: [ <expr1>, <expr2>, ... ] }
pieces → strings only (use $toString for numbers)
space → include " " as a literal if you need one
null → any null/missing piece → whole result null
fix → wrap fields with $ifNull(field, "")
for each document:
parts = evaluate each expression in [expr1, expr2, ...]
if any part is null or missing:
result = null
else:
result = join(parts) // no separator unless you included one
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while writing string-join expressions.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $concat: [ <a>, <b>, ... ] }
Input
Strings only
Null rule
Any null/missing → result null
Full-name pattern
$concat: ["$first", " ", "$last"]
Null-safe pattern
$ifNull: ["$middle", ""] inside the array
Sample check
"Ada" + " " + "Lovelace" → "Ada Lovelace"
Try it
Live Preview
Watch $concat join first name, a separator, and last name.
Simulates $concat with three string pieces (same idea as the mongosh examples below).
We use one person document. First we store the name parts, then we build a full name, then we build a label with a hyphen. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine one person: first name Ada, last name Lovelace. We save those two strings as one document in a collection named people.
After the insert, MongoDB keeps one document in people. Run find() to see it:
mongosh
db.people.find()
Stored in MongoDB
[
{
_id: 1,
first: "Ada",
last: "Lovelace"
}
]
Read that document like this:
Field in MongoDB
Value
Plain English
_id
1
Document id (row number for this lesson)
first
"Ada"
First name string
last
"Lovelace"
Last name string
Picture
Name parts stored separately:
first: "Ada"
last: "Lovelace"
Goal for the next examples:
join them into one display string
with either a space or a hyphen in between
How It Works
Insert once.insertOne adds a single person document to people.
MongoDB stores fields as written.first and last are just strings.
find() shows the same shape. What you inserted is what you get back—ready for $concat in the next examples.
Same stored document. Still using Ada / Lovelace from Example 1.
Change only the separator. Replace " " with "-" in the array.
Join again. Result becomes "Ada-Lovelace".
Easy check. Separators are just more string pieces—you control spacing by what you put in the array.
Edge Cases & Pitfalls
Check these before using $concat in production pipelines.
null
One null makes the whole result null
Missing or null fields do not skip themselves. Wrap with $ifNull and "" when you still want a partial string.
type
Strings only
Numbers and dates are not auto-converted. Use $toString (or $dateToString) first.
space
Spaces are not automatic
$concat does not insert separators. Add " ", "-", or ", " yourself as array items.
arrays
Not for array joining
To merge arrays, use $concatArrays. $concat is string-only.
Analysis
Performance Notes
Approach
Cost
Best for
$concat alone
Per-document string work
Display names, labels, keys
+ $ifNull wrappers
Small extra cost
Optional / sparse name parts
+ $toString
One conversion per value
Mixing numbers into labels
String joins are cheap compared with large sorts or joins. Filter early with $match when only some documents need a computed label.
Remember
Key Takeaways
Role: join string pieces into one string.
Separators: add " " or "-" yourself in the array.
Nulls: one null/missing piece → whole result null.
Types: strings only—convert with $toString first.
One line:$concat answers “glue these strings together in order”—and returns null if any piece is null.
Frequently Asked Questions
$concat joins multiple string values into one string. Pass an array of expressions—field references, string literals, or other string expressions—inside aggregation stages such as $project and $addFields.
The syntax is { $concat: [ <expression1>, <expression2>, ... ] }. Include as many pieces as you need, including literal separators like a space or hyphen.
If any expression evaluates to null or a missing field, $concat returns null for the whole result. Wrap risky fields with $ifNull to fall back to an empty string.
No. $concat only accepts strings. Convert numbers first with $toString, then pass them into $concat.
$concat joins strings into one string. $concatArrays joins arrays into one array. Same idea, different data types.
Yes in spirit. Think of $concat as MongoDB’s aggregation version of joining strings—run it in the pipeline, not as a standalone find() filter.