$concatArrays (Concatenate Arrays)
Last updated: 2026-10-09
$concatArrays (Concatenate Arrays)
$concatArrays merges multiple arrays into one without deduplication. As an array expression it is common in $project and $addFields; since MongoDB 8.1 it can also act as a $group accumulator, concatenating array fields across group documents. Unlike $setUnion, duplicates are kept.
| Category | Accumulators |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $concatArrays: [ <array1>, <array2>, ... ] }
Returns:New array of all input arrays concatenated (duplicates kept)
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $concatArrays: append a tag in $project
db.products.aggregate([
{ $project: { allTags: { $concatArrays: ['$tags', ['hot']] } } }
])
Output:
{ "_id": 1, "allTags": ["a", "b", "hot"] }
📌 Notes
- No deduplication — every element is kept
- All inputs must be arrays or the stage errors
- Usable as a $group accumulator since MongoDB 8.1
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QDoes $concatArrays deduplicate?
ANo; use $setUnion when deduplication is needed.
QWhat if an input is not an array?
AIt errors; all inputs must be arrays — check first with $isArray.
QCan it be used as a $group accumulator?
ASince MongoDB 8.1, yes — concatenating array fields across group documents.