The $concatArrays operator merges multiple arrays into one array in MongoDB aggregation pipelines. Use it to combine tags, skills, categories, or any list fields stored separately in your documents.
01
Merge Arrays
Join lists end-to-end.
02
Syntax
Array of array expressions.
03
Null Safe
Null → empty array.
04
$project Stage
Build unified lists.
05
Use Cases
Tags, skills, defaults.
06
vs $concat
Arrays vs strings.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $concatArrays operator concatenates multiple arrays into a single array. For example, { $concatArrays: [["js", "mongo"], ["node"]] } returns ["js", "mongo", "node"]. Elements keep their order: all items from the first array, then the second, and so on.
💡
Beginner Tip
Do not confuse $concatArrays with $concat. $concat joins strings into one string. $concatArrays joins arrays into one array.
Foundation
📝 Syntax
The $concatArrays operator takes an array of array expressions:
mongosh
{ $concatArrays: [ <array1>, <array2>, ... ] }
Syntax Rules
$concatArrays — returns one array containing all elements in order.
Arguments — field references ("$tags"), literal arrays ([1, 2]), or nested expressions.
Use inside stages like $project, $addFields, or $set.
null or missing array fields are treated as empty arrays.
Non-array values cause an aggregation error.
Does not flatten nested arrays — only merges at the top level.
Mouse: secondaryTags is null, treated as [] → only primary tags appear.
📈 Practical Patterns
Combine skills, append defaults, and merge three or more arrays.
Example 2 — Null and Missing Fields
When secondaryTags is null or missing, $concatArrays treats it as an empty array. The Laptop document merges four tags; the Mouse document keeps only its primary tags:
mongosh
// Mouse document: secondaryTags is null
{ "name": "Mouse", "allTags": ["electronics", "accessories"] }
// If secondaryTags were missing entirely, result is the same
This differs from $concat, where null makes the entire result null.
Example 3 — Combine Skills from Multiple Fields
Merge technical and soft skills into one list for search indexing:
Order is preserved: all elements from the first array, then all from the second. Use $setUnion afterward if you need unique values only.
Example 4 — Append a Default Array
Always add a default category tag to every product:
mongosh
db.products.aggregate([
{
$project: {
name: 1,
allTags: {
$concatArrays: [
"$primaryTags",
"$secondaryTags",
["catalog"]
]
}
}
}
])
// Every document gets "catalog" appended to its tag list
How It Works
Literal arrays work as arguments. This pattern is useful for injecting system tags, default permissions, or baseline categories into every merged result.
You can pass any number of arrays. Pair with $size to count total elements, or $setUnion to remove duplicates across the merged list.
Applications
🚀 Use Cases
Tag merging — combine primary and secondary tag arrays for unified search.
Skill lists — merge technical and soft skills into one profile array.
Default values — append system tags or baseline permissions to every document.
Report building — unify list fields from multiple sources before $unwind or grouping.
🧠 How $concatArrays Works
1
MongoDB resolves each array
Field references and literals are evaluated. Null or missing fields become empty arrays.
Input
2
$concatArrays merges in order
Elements from array 1 come first, then array 2, and so on. Nested arrays are not flattened.
Merge
3
The combined array is stored
The result is written to the field you define in $project or $addFields.
Output
=
📦
Single unified array
Ready for $unwind, $size, $setUnion, or further processing.
Wrap Up
Conclusion
The $concatArrays operator is the go-to tool for merging array fields in MongoDB aggregation pipelines. It preserves element order, handles null fields gracefully, and works with any number of input arrays.
For beginners, remember: use $concatArrays for arrays and $concat for strings. Use $setUnion when you need unique values after merging.