The $setIntersection operator returns elements that appear in every input array. Use it to find shared tags, overlapping skills, common permissions, or any values present in all sets.
01
Common Elements
In all arrays.
02
Array Syntax
Two+ arrays.
03
Set Semantics
Unique values.
04
Aggregation
$project / $addFields.
05
Use Cases
Match, overlap.
06
Set Family
Union, difference.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $setIntersection operator compares two or more arrays as sets and returns only the elements that appear in all of them. For example, { $setIntersection: [ [1, 2, 3], [2, 3, 4] ] } returns [2, 3].
Think of it as the overlap between sets — the Venn diagram center. If no elements are shared, the result is an empty array [].
💡
Beginner Tip
Need all elements from any array combined? Use $setUnion. Need elements in A but not B? Use $setDifference. Need the overlap? Use $setIntersection.
Foundation
📝 Syntax
The $setIntersection operator takes an array of two or more array expressions:
Combine $setIntersection, $size, and $gt inside $expr to filter documents with any overlap.
Bonus — Common to Three Permission Lists
Find permissions granted in all three sources:
mongosh
db.users.aggregate([
{
$project: {
name: 1,
commonPermissions: {
$setIntersection: [
"$dbRoles",
"$ldapGroups",
"$policyGrants"
]
}
}
}
])
// Only permissions present in dbRoles AND
// ldapGroups AND policyGrants
How It Works
Pass three or more arrays. An element must appear in every array to be included in the result.
Applications
🚀 Use Cases
Recommendations — score content by shared tags or categories.
Recruitment — highlight skills that match job requirements.
Access control — find permissions common across role sources.
Data validation — verify overlap between expected and actual value sets.
🧠 How $setIntersection Works
1
MongoDB evaluates each array
All arguments resolve to arrays (field paths, literals, or nested expressions).
Input
2
Sets are compared for overlap
MongoDB finds values that appear in every input array, ignoring duplicates.
Intersect
3
Shared elements are collected
The result array contains only values present in all sets. No overlap yields [].
Output
=
🔗
Intersection array
Use with $size for scores or $match for filtering.
Wrap Up
Conclusion
The $setIntersection operator finds elements shared by all input arrays using set semantics. It is the go-to tool for overlap, matching, and relevance scoring inside aggregation pipelines.
Pair it with $size to count overlaps, or with $match and $expr to filter documents that share values. Next in the series: $setIsSubset.
Use $setIntersection for overlap and matching logic
Combine with $size for numeric relevance scores
Handle null arrays with $ifNull when fields may be missing
Use $setUnion when you need all unique elements combined
Normalize string casing before comparing tag-like values if needed
❌ Don’t
Expect ordered list intersection — this is set semantics
Confuse intersection (in all) with union (in any)
Forget that null input arrays return null
Assume duplicates in the result — each value appears once
Use $setEquals when you need the shared elements array itself
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $setIntersection
Use these points when finding shared array elements in pipelines.
5
Core concepts
🔗01
Overlap
In all arrays.
Purpose
📝02
[ A, B, ... ]
Two+ arrays.
Syntax
🛠03
Unique only
Set semantics.
Output
🗃04
+ $size
Count overlap.
Chaining
⚠05
[]
No shared values.
Edge case
❓ Frequently Asked Questions
$setIntersection returns elements that appear in every input array. It treats arrays as sets, so duplicates are ignored. For example, { $setIntersection: [ [1, 2, 3], [2, 3, 4] ] } returns [2, 3].
The syntax is { $setIntersection: [ <array1>, <array2>, ... ] }. Each argument is an array expression. You need at least two arrays. The result contains only values present in all of them.
The result uses set semantics. Element order generally follows the order of appearance in the first array, but only common elements are kept. Duplicates in input arrays are ignored.
$setIntersection returns elements in all arrays (the overlap). $setUnion returns every unique element from any array (the combined set).
$setIntersection returns the shared elements as an array. $setEquals returns true or false depending on whether two or more arrays contain exactly the same set of elements.