The $setUnion operator merges two or more arrays into a single list of unique elements. Use it to combine tags, roles, skills, or permissions from multiple sources without duplicates.
01
Merge Arrays
Combine sets.
02
Remove Dupes
Unique only.
03
Two+ Arrays
Any number.
04
Aggregation
$project / $addFields.
05
Use Cases
Roles, tags.
06
Set Family
Intersect, diff.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $setUnion operator takes two or more arrays and returns every unique element found in any of them. For example, { $setUnion: [ [1, 2, 3], [2, 3, 4] ] } returns [1, 2, 3, 4].
Think of it as combining Venn diagram circles into one set — every value from every array is included, but each value appears only once in the result.
💡
Beginner Tip
Need only shared elements? Use $setIntersection. Need elements in A but not B? Use $setDifference. Need everything combined uniquely? Use $setUnion.
Foundation
📝 Syntax
The $setUnion operator takes an array of two or more array expressions:
Use $concatArrays when order and duplicates matter (like concatenating log entries). Use $setUnion when you need a unique membership list.
Applications
🚀 Use Cases
Access control — merge roles or permissions from multiple identity sources.
Content tagging — combine tags from users, articles, and categories.
Skill profiles — build a deduplicated skill list from assessments and certifications.
Data enrichment — unify values from joined collections without duplicate entries.
🧠 How $setUnion Works
1
MongoDB evaluates each array
All arguments resolve to arrays (field paths, literals, or nested expressions).
Input
2
All elements are collected
Every value from every input array is gathered into one combined set.
Collect
3
Duplicates are removed
Set semantics ensure each value appears only once in the final result array.
Dedupe
=
🔗
Union array
Use with $size for counts or store as an enriched field.
Wrap Up
Conclusion
The $setUnion operator merges multiple arrays into one deduplicated set. It is the standard tool for combining roles, tags, skills, or permissions from different sources inside aggregation pipelines.
Pair it with $size to count unique combined values. For overlap checks, use $setIntersection; for subtraction, use $setDifference. Next in the series: $sin.
Use $setUnion when you need a unique combined membership list
Combine with $size to count distinct combined values
Handle null arrays with $ifNull when fields may be missing
Use $setIntersection when you only need shared elements
Normalize string casing before merging tag-like values if needed
❌ Don’t
Use $setUnion when you need to preserve duplicate entries
Confuse union (all unique) with intersection (in all arrays)
Forget that null input arrays return null
Expect strict ordering across sources — order follows set rules
Use $concatArrays when deduplication is required
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $setUnion
Use these points when merging arrays as sets in pipelines.
5
Core concepts
🔗01
Combine
All unique values.
Purpose
📝02
[ A, B, ... ]
Two+ arrays.
Syntax
🛠03
No dupes
Set semantics.
Output
🗃04
+ $size
Count unique.
Chaining
⚠05
vs $concat
Dedupe vs keep.
Compare
❓ Frequently Asked Questions
$setUnion combines two or more arrays and returns every unique element from all of them. Duplicates are removed using set semantics. For example, { $setUnion: [ [1, 2, 3], [2, 3, 4] ] } returns [1, 2, 3, 4].
The syntax is { $setUnion: [ <array1>, <array2>, ... ] }. Each argument is an array expression. You need at least two arrays. The result contains all unique values from every input array.
Yes. $setUnion treats arrays as sets. If the same value appears in multiple input arrays or multiple times within one array, it appears only once in the result.
$setUnion returns all unique elements from any input array (the combined set). $setIntersection returns only elements that appear in every input array (the overlap).
$concatArrays joins arrays end-to-end and keeps duplicates. $setUnion merges arrays as sets and removes duplicates. Use $setUnion when you need a unique combined list.