User Avatar

John Lynn

10mo ago

Author, educator, lifelong learner, and enthusiast of all things related to data storage and management technologies!

Merging arrays in MongoDB aggregations just got easier
John Lynn

Suppose that you're working on an e-commerce platform where products have multiple tags, and you need to generate a report showing all tags used within each product category. You want a single flat array per category—not nested arrays, not duplicates necessarily removed, but a straightforward merge of all tag arrays.

This is a common pattern, and until MongoDB 8.1, it required a workaround. Let's look at the old approach, then the cleaner solution now available.

The scenario

Here's our sample data:

db.products.insertMany([
  { name: "Running Shoes", category: "footwear", tags: ["athletic", "lightweight", "breathable"] },
  { name: "Hiking Boots", category: "footwear", tags: ["outdoor", "waterproof", "durable"] },
  { name: "Sandals", category: "footwear", tags: ["casual", "summer"] },
  { name: "Winter Jacket", category: "outerwear", tags: ["warm", "waterproof", "insulated"] }
])

We want all tags for each category combined into one array.

Before MongoDB 8.1

The standard approach used $push to collect arrays, then $reduce to flatten them. Like so:

db.products.aggregate([
  {
    $group: {
      _id: "$category",
      collectedTags: { $push: "$tags" }
    }
  },
  {
    $project: {
      allTags: {
        $reduce: {
          input: "$collectedTags",
          initialValue: [],
          in: { $concatArrays: ["$$value", "$$this"] }
        }
      }
    }
  }
])

The result after the first stage would be an array of arrays:

{ _id: "footwear", collectedTags: [["athletic", "lightweight", "breathable"], ["outdoor", "waterproof", "durable"], ["casual", "summer"]] }

Then the $reduce flattens it. This works, but the two-stage dance for something conceptually straightforward doesn't feel right.

MongoDB 8.1: $concatArrays as an accumulator

Enter the new capability. In MongoDB 8.1, $concatArrays works directly as an accumulator in $group. The query now looks like this:

db.products.aggregate([
  {
    $group: {
      _id: "$category",
      allTags: { $concatArrays: "$tags" }
    }
  }
])

The result:

{ _id: "footwear", allTags: ["athletic", "lightweight", "breathable", "outdoor", "waterproof", "durable", "casual", "summer"] }
{ _id: "outerwear", allTags: ["warm", "waterproof", "insulated"] }

Notice that the three footwear tag arrays merge directly into one flat array. One stage, clear intent.

Where you can use it

The $concatArrays accumulator works in:

  • $group

  • $bucket

  • $bucketAuto

A note on duplicates

What this does is concatenate—it doesn't deduplicate. In our result, if two products had the same tag, it would appear twice. If you need unique values, you'd still use $addToSet instead—though $addToSet with arrays has its own behavior (it treats each array as a single value).

For true deduplication of merged arrays, you might follow $concatArrays with a $setUnion in a $project stage. But for many use cases—like our tag report—duplicates are acceptable or even informative.

So there you have it—cleaner array merging in MongoDB 8.1 aggregations!