The Wayback Machine - https://web.archive.org/web/20250306122709/https://www.geeksforgeeks.org/mongodb-concatarrays-operator/
Open In App

MongoDB $concatArrays Operator

Last Updated : 05 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The $concatArrays operator in MongoDB is one of the many array expression operators available for use in aggregation pipeline stages. It is particularly useful for combining arrays from different fields or documents within a collection.

In this article, We will learn about the MongoDB $concatArrays in detail by understanding various examples and so on.

MongoDB $concatArrays Operator

  • MongoDB provides different types of array expression operators that are used in the aggregation pipeline stages and the $concatArrays operator is one of them.
  • This operator is used to concatenate two or more arrays and return a concatenated array.

Syntax: 

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

Here, the array must be a valid expression until it resolves to an array. If the argument of this operator is a missing field or the argument resolve to a null value, then this operator will give null.

Examples of MongoDB $concatArrays

In the following examples, we are working with:

Database: GeeksforGeeks

Collection: arrayExample

Document: three documents that contain the details in the form of field-value pairs.

Output:

Example 1: Using $concatArrays Operator

In this example, we are going to concatenate the values(i.e., arrays) of  numbers1 and numbers2 fields using $concatArrays operator.

db.arrayExample.aggregate([
... {$match: {name: "Lolo"}},
... {$project: {
... concatResult: {$concatArrays: ["$numbers1", "$numbers2"]}}}])

Output:

Example 2: Using $concatArrays Operator to concate arrays Values

In this example, we are going to concatenate the values(i.e., arrays) of fruits and veggie fields using $concatArrays operator.

db.arrayExample.aggregate([
... {$match: {name: "Bongo"}},
... {$project: {concatResult:
... {$concatArrays: ["$fruits", "$veggie"]}}}])

Output:

Example 3: Using $concatArrays Operator in the Embedded Document

In this example, we are going to concatenate the value(i.e., array) of favGame.indoorGames field with the specified array(i.e., [“Uno”, “Snooker”]) using $concatArrays operator.

db.arrayExample.aggregate([{$match: {name: "Piku"}},
... {$project: {concatResult: {
... $concatArrays: ["$favGame.indoorGames", ["Uno", "Snooker"]]}}}])

Output:

Using-$concatArrays-Operator-in-the-Embedded-Document

Conclusion

The $concatArrays operator in MongoDB is a powerful tool for concatenating arrays within your documents, allowing for more flexible and comprehensive data analysis. By mastering this operator, you can efficiently manage and manipulate array data across different fields and documents in your collections.

FAQs on MongoDB $concatArrays

1. What is the $concatArrays operator used for in MongoDB?

The $concatArrays operator in MongoDB is used to concatenate two or more arrays and return the concatenated array. It is useful in aggregation pipelines to merge array data from different fields or documents.

2. What happens if one of the arrays in $concatArrays is missing or null?

If one of the arrays in $concatArrays is a missing field or resolves to a null value, the operator will return null.

3. Can $concatArrays be used with embedded documents?

Yes, $concatArrays can be used with embedded documents. You can specify arrays within embedded documents and concatenate them with other arrays.

4. Is it possible to concatenate more than two arrays using $concatArrays?

Yes, it is possible to concatenate more than two arrays using $concatArrays. You can include multiple arrays within the operator’s array argument, and it will concatenate all of them


Next Article

Similar Reads

three90RightbarBannerImg