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

# $setDifference  (expression operator)

## Definition

Takes two sets and returns an array containing the elements that only exist in the first set; i.e. performs a [relative complement](http://en.wikipedia.org/wiki/Complement_\(set_theory\)) of the second set relative to the first.

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

```javascript
{ $setDifference: [ <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

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

[`$setDifference`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setdifference.md#mongodb-expression-exp.-setDifference) filters out duplicates in its result to output an array that contains only unique entries. The order of the elements in the output array is unspecified.

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

| Example | Result | Notes |
| --- | --- | --- |
| `{ $setDifference: [
     [ "a", "c" ],
     [ "a", "b" ]
] }` | `[ "c" ]` | `"a"` is present in both arrays, so it is removed from the result. `"c"` is only present in the first array. |
| `{ $setDifference: [
     [ "a", "c" ],
     [ "a", "b", "c" ]
] }` | `[ ]` | Both `"a"` and `"c"` are present in both arrays so they are removed from the result. No more elements remain in the first array. |
| `{ $setDifference: [
     [ "a", "b", "a" ],
     [ "b", "a" ]
] }` | `[ ]` | Both `"a"` and `"b"` are present in both arrays so they are removed from the result. Duplicates are also removed. No more elements remain in the first array. |
| `{ $setDifference: [
     [ "a", "b" ],
     [ [ "a", "b" ] ]
] }` | `[ "a", "b" ]` | The strings `"a"` and `"b"` from the first array are not found in the second array, which contains a nested array. |
| `{ $setDifference: [
     [ ],
     [ "a", "b" ]
] }` | `[ ]` | The first array is empty so there are no elements to subtract from, regardless of what's in the second array. |
| `{ $setDifference: [
     [ "a", "a" ],
     [ "a", "b" ]
] }` | `[ ]` | `"a"` is present in the second array so it is removed from the result. Duplicates are also removed. No more elements remain in the first array. |

## Example

Consider an `flowers` collection with the following documents:

```javascript
db.flowers.insertMany( [
   { "_id" : 1, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid" ] },
   { "_id" : 2, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "orchid", "rose", "orchid" ] },
   { "_id" : 3, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid", "jasmine" ] },
   { "_id" : 4, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "jasmine", "rose" ] },
   { "_id" : 5, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ ] },
   { "_id" : 6, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose" ], [ "orchid" ] ] },
   { "_id" : 7, "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose", "orchid" ] ] },
   { "_id" : 8, "flowerFieldA" : [ ], "flowerFieldB" : [ ] },
   { "_id" : 9, "flowerFieldA" : [ ], "flowerFieldB" : [ "rose" ] }
] )
```

The following operation uses the [`$setDifference`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setdifference.md#mongodb-expression-exp.-setDifference) operator to return an array of elements found in the `flowerFieldB` array but *not* in the `flowerFieldA` array:

```javascript
db.flowers.aggregate(
   [
     { $project: { flowerFieldA: 1, flowerFieldB: 1, inBOnly: { $setDifference: [ "$flowerFieldB", "$flowerFieldA" ] }, _id: 0 } }
   ]
)
```

The operation returns the following results:

```javascript
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid" ], "inBOnly" : [ ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "orchid", "rose", "orchid" ], "inBOnly" : [ ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "rose", "orchid", "jasmine" ], "inBOnly" : [ "jasmine" ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ "jasmine", "rose" ], "inBOnly" : [ "jasmine" ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ ], "inBOnly" : [ ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose" ], [ "orchid" ] ], "inBOnly" : [ [ "rose" ], [ "orchid" ] ] }
{ "flowerFieldA" : [ "rose", "orchid" ], "flowerFieldB" : [ [ "rose", "orchid" ] ], "inBOnly" : [ [ "rose", "orchid" ] ] }
{ "flowerFieldA" : [ ], "flowerFieldB" : [ ], "inBOnly" : [ ] }
{ "flowerFieldA" : [ ], "flowerFieldB" : [ "rose" ], "inBOnly" : [ "rose" ] }
```
