> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $setEquals  (expression operator)

## Definition

Compares two or more arrays and returns `true` if they have the same distinct elements and `false` otherwise.

[`$setEquals`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setequals.md#mongodb-expression-exp.-setEquals) has the following syntax:

```javascript
{ $setEquals: [ <expression1>, <expression2>, ... ] }
```

The arguments can be any valid [expression](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) as long as they each resolve to an array. For more information on expressions, see [Expressions.](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions)

## Behavior

[`$setEquals`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setequals.md#mongodb-expression-exp.-setEquals) performs set operation on arrays, treating arrays as sets. If an array contains duplicate entries, [`$setEquals`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setequals.md#mongodb-expression-exp.-setEquals) ignores the duplicate entries. [`$setEquals`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setequals.md#mongodb-expression-exp.-setEquals) ignores the order of the elements.

If a set contains a nested array element, [`$setEquals`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setequals.md#mongodb-expression-exp.-setEquals) does *not* descend into the nested array but evaluates the array at top-level.

| Example | Result | Notes |
| --- | --- | --- |
| `{ $setEquals: [
  [ "a", "c" ],
  [ "a", "b" ]
] }` | `false` | The arrays do not contain the same elements. |
| `{ $setEquals: [
  [ "a", "c" ],
  [ "a", "b", "c" ]
] }` | `false` | The arrays do not contain the same elements. |
| `{ $setEquals: [
  [ "a", "b", "a" ],
  [ "b", "a" ]
] }` | `true` | Both arrays contain the same unique elements. When treated as sets, the first array is `[ "a", "b" ]` because duplicates are ignored. |
| `{ $setEquals: [
  [ "a", "b" ],
  [ [ "a", "b" ] ]
] }` | `false` | The elements in the arrays are not equal. The first array contains the strings `"a"` and `"b"`, while the second contains an array `[ "a", "b" ]`. |
| `{ $setEquals: [
  [ ],
  [ "a", "b" ]
] }` | `false` | The elements in the arrays are not equal. The first array is empty and the second array has two string elements. |
| `{ $setEquals: [
  [ "a", "a" ],
  [ "a", "b" ]
] }` | `false` | The arrays do not contain the same unique elements. The first array only contains `"a"`, while the second array contains `"a"` and `"b"`. |

## Example

Consider a `bakeryOrders` collection with the following documents:

```javascript
db.bakeryOrders.insertMany( [
   { _id: 0, cakes: ["chocolate", "vanilla"], cupcakes: ["chocolate", "vanilla"] },
   { _id: 1, cakes: ["chocolate", "vanilla"], cupcakes: ["vanilla", "chocolate"] },
   { _id: 2, cakes: ["chocolate", "chocolate"], cupcakes: ["chocolate"] },
   { _id: 3, cakes: ["vanilla"], cupcakes: ["chocolate"] },
   { _id: 4, cakes: ["vanilla"], cupcakes: [] }
] )
```

The following operation uses the [`$setEquals`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setequals.md#mongodb-expression-exp.-setEquals) operator to determine if the `cakes` array and the `cupcakes` array in each order contain the same flavors:

```javascript
db.bakeryOrders.aggregate(
   [
      {
         $project: {
            _id: 0,
            cakes: 1,
            cupcakes: 1,
            sameFlavors: { $setEquals: [ "$cakes", "$cupcakes" ] }
         }
      }
   ] )
```

**Note: $project**

The [`$project`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/project.md#mongodb-pipeline-pipe.-project) stage specifies which fields are included in the output documents. In this example, the [`$project`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/project.md#mongodb-pipeline-pipe.-project) stage:

- Excludes the `_id` field from the output.

- Includes the `cakes` and `cupcakes` fields in the output.

- Outputs the result of the `$setEquals` operator in a new field called `sameFlavors`.

The operation returns the following results:

```javascript
{
 cakes: [ "chocolate", "vanilla" ],
 cupcakes: [ "chocolate", "vanilla" ],
 sameFlavors: true
},
{
 cakes: [ "chocolate", "vanilla" ],
 cupcakes: [ "vanilla", "chocolate" ],
 sameFlavors: true
},
{
 cakes: [ "chocolate", "chocolate" ],
 cupcakes: [ "chocolate" ],
 sameFlavors: true
},
{
 cakes: [ "vanilla" ],
 cupcakes: [ "chocolate" ],
 sameFlavors: false
},
{
   cakes: [ "vanilla" ],
   cupcakes: [],
   sameFlavors: false
}
```
