The $setIsSubset operator checks whether the first array is a subset of the second — every element in array1 must also appear in array2. Use it for role validation, skill requirements, tag whitelists, and permission checks.
01
Subset Check
A contained in B?
02
Boolean
true or false.
03
Two Arrays
Exactly two inputs.
04
Set Semantics
Order ignored.
05
Use Cases
Roles, skills, tags.
06
Set Family
Equals, union.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $setIsSubset operator compares two arrays as sets and returns true when every element in the first array is also present in the second. For example, { $setIsSubset: [ [1, 2], [1, 2, 3, 4] ] } returns true because 1 and 2 are both in the second array.
Read it as “Is A contained in B?” — not the other way around. If A has an element that B lacks, the result is false. An empty first array is always a subset of any second array.
💡
Beginner Tip
To verify a candidate has all required skills, put requiredSkills first and candidateSkills second: { $setIsSubset: [ "$requiredSkills", "$candidateSkills" ] }.
Foundation
📝 Syntax
The $setIsSubset operator takes exactly two array expressions:
mongosh
{ $setIsSubset: [ <array1>, <array2> ] }
Literal Examples
mongosh
{ $setIsSubset: [ [1, 2], [1, 2, 3, 4] ] }
// true — 1 and 2 are both in the second array
{ $setIsSubset: [ [1, 2, 5], [1, 2, 3, 4] ] }
// false — 5 is not in the second array
{ $setIsSubset: [ [], [1, 2, 3] ] }
// true — empty set is subset of any set
{ $setIsSubset: [ [1, 2], [2, 1] ] }
// true — order does not matter
Syntax Rules
Exactly two arrays — first is tested against second.
Return value — true if every element of array1 is in array2; otherwise false.
Set semantics — order and duplicates in either array are ignored.
Empty first array — always returns true.
Returns null if either input array is null.
Use inside $project, $addFields, $match (with $expr), or $cond.
Combine with $cond for audit reports and admin dashboards that need human-readable status text.
Applications
🚀 Use Cases
Access control — verify user roles are within an allowed role set.
Recruitment — confirm candidates have all required skills.
Content moderation — ensure tags or categories stay within a whitelist.
Policy compliance — check permission arrays are contained in policy grants.
🧠 How $setIsSubset Works
1
MongoDB evaluates two arrays
Both arguments resolve to arrays (field paths, literals, or nested expressions).
Input
2
Each array is treated as a set
Duplicates are ignored and order does not matter for either array.
Normalize
3
Every element of A is checked against B
If any value in the first array is missing from the second, the result is false.
Compare
=
✅
true or false
Use in projections, filters, or conditional logic.
Wrap Up
Conclusion
The $setIsSubset operator checks whether the first array is contained in the second using set semantics. It returns true when every element of array1 appears in array2.
Use it for role validation, skill requirements, and tag whitelists. Remember argument order: put the smaller or required set first. Next in the series: $setUnion.
Use $setIntersection when you only need a boolean answer
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $setIsSubset
Use these points when checking whether one array is contained in another.
5
Core concepts
🔍01
A ⊆ B?
Contained in B.
Purpose
📝02
[ A, B ]
Exactly two.
Syntax
🛠03
Order matters
First ⊆ second.
Rule
🗃04
[] first
Always true.
Edge case
⚠05
vs $setEquals
Subset vs equal.
Compare
❓ Frequently Asked Questions
$setIsSubset returns true when every element in the first array also appears in the second array. It treats arrays as sets, so order and duplicates are ignored. For example, { $setIsSubset: [ [1, 2], [1, 2, 3, 4] ] } returns true.
The syntax is { $setIsSubset: [ <array1>, <array2> ] }. It takes exactly two array expressions. The result is true when array1 is a subset of array2 (every value in array1 is contained in array2).
Yes. The empty array [] is a subset of every array, so { $setIsSubset: [ [], [1, 2, 3] ] } returns true. This follows standard set theory.
$setIsSubset checks whether the first array is contained in the second (A ⊆ B). $setEquals checks whether two or more arrays contain exactly the same set of elements. Equal sets satisfy both directions of subset.
$setIsSubset returns true or false. $setIntersection returns the actual shared elements as an array. If $setIsSubset [A, B] is true, then $setIntersection [A, B] equals A (as sets).