$setEquals (Check Set Equality)

Last updated: 2026-10-09

$setEquals (Check Set Equality)

$setEquals checks whether two or more arrays are equal as sets, ignoring order and duplicates. Typical uses: verifying that two permission lists match or comparing a config with defaults. It accepts multiple arguments and returns true only when all arrays are set-equal. null elements are ignored, so arrays containing null compare as if the null were absent. Order does not matter: [1, 2] and [2, 1] are equal.

CategorySet, Condition & Conversion
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $setEquals: [ <array1>, <array2>, ... ] }

⚙️ Parameters

arrayAt least two arrays to compare.

Returns:true when all arrays are set-equal, otherwise false

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $setEquals: do two permission lists match
db.roles.aggregate([
  { $project: {
      samePerms: { $setEquals: ['$userPerms', '$defaultPerms'] }
  } }
])

Output:

{ '_id': 1, 'samePerms': true }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QDoes order affect the result?
ANo — set comparison ignores order.
QHow many arguments are supported?
AAt least two; true only when all are set-equal.
QWhat about arrays containing null?
Anull elements are ignored, so the result matches a version without them.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏