The $setEquals operator checks whether two or more arrays contain the same set of elements and returns true or false. Order and duplicates are ignored — perfect for comparing roles, tags, or permissions.
01
Set Compare
Same elements?
02
Boolean
true or false.
03
Order Free
[1,2] = [2,1].
04
Two+ Arrays
All must match.
05
Use Cases
Roles, tags, ACLs.
06
vs $eq
Set vs ordered.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $setEquals operator compares two or more arrays as sets. It returns true when every array contains exactly the same unique elements, and false when they differ. For example, { $setEquals: [ [1, 2, 3], [3, 2, 1] ] } returns true.
This is different from $eq, which compares arrays in order. Use $setEquals when you care about membership, not sequence — such as verifying that a user’s roles match a template or that two tag lists are equivalent.
💡
Beginner Tip
Duplicates are ignored: [1, 1, 2] and [2, 1] are equal sets. If you need exact ordered array equality, use $eq instead.
Foundation
📝 Syntax
The $setEquals operator takes an array of two or more array expressions:
mongosh
{ $setEquals: [ <array1>, <array2>, ... ] }
Literal Examples
mongosh
{ $setEquals: [ [1, 2, 3], [3, 2, 1] ] }
// true — same set, different order
{ $setEquals: [ ["admin", "user"], ["user", "admin"] ] }
// true
{ $setEquals: [ [1, 2], [2, 3] ] }
// false — different elements
{ $setEquals: [ [1, 2], [1, 2], [2, 1] ] }
// true — all three match
Syntax Rules
Minimum arrays — at least two array expressions required.
Return value — true if all arrays represent the same set; otherwise false.
Order ignored — [a, b] equals [b, a].
Duplicates ignored — set semantics apply to each array.
Returns null if any input array is null.
Use inside $project, $addFields, $match (with $expr), or $cond.
$cond uses the boolean from $setEquals as its condition. This pattern works well in audit and access-control reports.
Bonus — Compare Three Arrays at Once
Verify that three role sources all agree:
mongosh
db.users.aggregate([
{
$project: {
name: 1,
allSourcesAgree: {
$setEquals: [
"$roles",
"$ldapRoles",
"$expectedRoles"
]
}
}
}
])
// true only when all three arrays are the same set
How It Works
Pass more than two arrays. Every array must represent the same set for the result to be true.
Applications
🚀 Use Cases
Access control — verify user roles match expected or template roles.
Tag validation — confirm product or content tags match category requirements.
Data sync checks — compare arrays from different sources (LDAP, DB, cache).
Compliance reporting — flag documents where permission sets diverge from policy.
🧠 How $setEquals Works
1
MongoDB evaluates each array
All arguments resolve to arrays (field paths, literals, or nested expressions).
Input
2
Each array is treated as a set
Duplicates are removed and order is ignored for comparison purposes.
Normalize
3
All sets are compared
If every array contains exactly the same unique elements, the result is true.
Compare
=
✅
true or false
Use in projections, filters, or conditional logic.
Wrap Up
Conclusion
The $setEquals operator compares arrays as sets and returns true when they contain the same unique elements. Order and duplicates do not affect the result.
Use it for role checks, tag validation, and sync verification. For elements in A but not B, use $setDifference. Next in the series: $setField.
Use $setEquals when order and duplicates should not matter
Wrap in $expr when filtering with $match
Use $ifNull to treat missing arrays as [] when appropriate
Compare three or more sources in one expression when they must all agree
Use $eq when exact ordered array equality is required
❌ Don’t
Expect [1, 2] and [2, 1] to differ — they are equal sets
Use $setEquals for ordered list comparison (use $eq)
Forget that null input arrays return null, not false
Assume case-insensitive string matching unless values are normalized
Confuse set equality with subset checks (see $setIsSubset in MongoDB docs)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $setEquals
Use these points when comparing arrays as sets in pipelines.
5
Core concepts
✅01
Same Set?
Boolean check.
Purpose
📝02
[ A, B, ... ]
Two+ arrays.
Syntax
🛠03
Order ignored
Set semantics.
Rule
🗃04
vs $eq
Set vs ordered.
Compare
⚠05
null in
null out.
Edge case
❓ Frequently Asked Questions
$setEquals returns true when all input arrays contain the same set of elements, and false otherwise. Order and duplicate values are ignored — [1, 2, 3] and [3, 2, 1] are considered equal.
The syntax is { $setEquals: [ <array1>, <array2>, ... ] }. You can compare two or more arrays. Each argument is an array expression (field path or literal).
No. $setEquals uses set semantics. Element order does not matter. Only which unique values are present counts.
$eq compares arrays element-by-element in order — [1, 2] and [2, 1] are not equal with $eq. $setEquals treats arrays as sets, so order and duplicates are ignored.
$setEquals checks whether arrays represent the same set (boolean). $setDifference returns elements in the first array but not others. $setIntersection returns shared elements. If $setDifference [A, B] is empty and $setDifference [B, A] is empty, the sets are equal.