$setEquals

Returns true if two arrays contain the same distinct elements, regardless of order or duplicates, performing set equality comparison.

Syntax

{ $setEquals: [<array_expression1>, <array_expression2>] }

Parameters

array_expression1objectrequired

The first array expression to compare

array_expression2objectrequired

The second array expression to compare

Examples

Compare array equality

Check if two arrays contain the same unique elements

Query:

db.products.aggregate([{ $project: { tagsMatch: { $setEquals: ["$currentTags", "$expectedTags"] } } }])

Output:

Documents with tagsMatch boolean indicating if tag arrays are equivalent

Validate array contents

Verify that array contents match expected values

Query:

db.orders.aggregate([{ $project: { itemsComplete: { $setEquals: ["$orderedItems", "$deliveredItems"] } } }])

Output:

Documents with itemsComplete showing if ordered and delivered items match

Compare user roles

Check if user has exact set of required roles

Query:

db.users.aggregate([{ $project: { rolesMatch: { $setEquals: ["$userRoles", "$requiredRoles"] } } }])

Output:

Documents with rolesMatch indicating if user roles exactly match requirements

Related