$concatArrays (Concatenate Arrays)

Last updated: 2026-10-09

$concatArrays (Concatenate Arrays)

$concatArrays merges two or more arrays into a single new array. It is commonly used to combine tag lists, history records, or paged data. Every argument must resolve to an array; if any argument is null or missing the whole result is null. It never mutates the source arrays, returning a fresh merged array, which makes it ideal for composing fields inside $addFields or $project. Unlike $concat, which only joins strings, $concatArrays works exclusively with arrays.

CategoryArray
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $concatArrays: [ <array1>, <array2>, ... ] }

⚙️ Parameters

arrayArray expressions to merge; at least two.

Returns:A new merged array; null if any argument is null or missing

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $concatArrays: merge tags and aliases
db.products.aggregate([
  { $project: {
      allTags: { $concatArrays: ['$tags', '$aliases'] }
  } }
])

Output:

{ '_id': 1, 'allTags': [ 'a', 'b', 'c', 'd' ] }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QCan $concatArrays join strings?
ANo, it only accepts arrays. Join strings with $concat, or split them into arrays first.
QWhat happens if an argument is null?
AThe whole result becomes null. Replace null with an empty array via $ifNull.
QHow is $concatArrays different from $push?
A$push accumulates elements in $group, while $concatArrays merges existing arrays inside an expression.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏