$setEquals is a MongoDB aggregation expression that returns true when two or more arrays contain the same unique elements as sets. Order and duplicates do not matter.
Remember
$setEquals = same unique elements?
{ $setEquals: [ <array1>, <array2>, ... ] }
[1, 2, 3] vs [3, 2, 1] → true
[1, 2, 2] vs [2, 1] → true
[1, 2] vs [1, 2, 3] → false
order ignored · duplicates ignored
null / missing arg → null
vs $eq: order matters for $eq
for each document:
arrays = evaluate each argument
if any array is null or missing:
result = null
else:
sets = unique(elements) for each array
result = (all sets are identical)
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while comparing arrays as sets.
Topic
Detail
Type
Aggregation expression (returns boolean)
Syntax
{ $setEquals: [ a, b, ... ] }
Order
Ignored
Duplicates
Ignored
vs $eq
Set equality vs positional equality
Null arg
Result null
Sample
["quiz","math"] ≡ ["math","quiz"]
Try it
Live Preview
Compare two arrays with set rules (order and duplicates ignored).
Simulates { $setEquals: [a, b] } and shows $eq for contrast.
Result
a = ["quiz", "math"]
b = ["math", "quiz"]
$setEquals → true
$eq → false
(reason: same unique values; different order)
Trace
Worked Walkthrough — Required Skills
Asha has skills math and quiz. The required list is the same skills in a different order. As sets, they match.
We use one student with a skills list and a required list. First insert the sample, then compare with $setEquals, then contrast with $eq. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine student Asha with skills ["math", "quiz"] and required ["quiz", "math"]. We save the document in students.
Array view.$eq checks index 0: "math" ≠ "quiz" → false.
Pick the right tool. Use sets when membership matters; use $eq when sequence matters.
Edge Cases & Pitfalls
Check these before treating arrays as sets in production.
order
Order never matters
If sequence is part of the business rule, use $eq, not $setEquals.
dupes
Duplicates are ignored
[1, 1, 2] equals [2, 1]. Counts do not matter—only unique membership.
null
Null args → null
A missing or null array makes the whole result null, not false.
type
Arguments must be arrays
Non-array inputs error. Validate or default with $ifNull when fields may be missing.
Analysis
Performance Notes
Approach
Cost
Best for
$setEquals on small arrays
Cheap set compare
Tags / skills checks
Huge arrays
Grows with unique size
Keep lists lean
$eq when order matters
Simple positional check
Exact array equality
Comparing short tag lists is inexpensive. Filter early with $match when only some documents need the check.
Remember
Key Takeaways
Role: same unique elements across arrays?
Ignores: order and duplicates.
vs $eq: membership vs exact sequence.
Sample:["math","quiz"] ≡ ["quiz","math"].
One line:$setEquals answers “do these arrays hold the same unique values, regardless of order?”
Frequently Asked Questions
$setEquals returns true when the input arrays contain the same set of unique elements, and false otherwise. Order and duplicate values are ignored—so [1, 2, 3] and [3, 2, 1] are equal.
Use { $setEquals: [ <array1>, <array2>, ... ] }. You can compare two or more arrays. Each argument must be an array expression (field path or literal).
No. It uses set semantics. Only which unique values are present matters, not order or how many times a value repeats.
$eq compares arrays position by position, so [1, 2] and [2, 1] are not equal. $setEquals treats them as the same set and returns true.
If any argument is null or refers to a missing field, $setEquals returns null.
Yes. Pass three or more arrays. All of them must represent the same set for the result to be true.