w3resource

MongoDB Aggregation Set Operator - $setDifference

MongoDB: $setDifference

The MongoDB $setDifference operators takes exactly two argument expressions i.e. two sets and returns an array containing the elements that only exist in the first set; i.e. performs a relative complement of the second set relative to the first.

Syntax :

{ $setDifference: [ <expression1>, <expression2> ] }
  • The $setDifference operator can only performs set operation on arrays and treated as sets. If any duplicate value contains in an array, it ignores by $setDifference operator. The order of the elements in not important in this operation..
  • Only the unique value in an array appears as a result after filtering the duplicates values in the $setDifference operation.
  • When a set contains a nested array element, the $setDifference does not descend into the nested array but evaluates the array at top-level.

Sample collection test_collection

{ "_id" : 1, "A" : [ "cat", "rat" ], "B" : [ "cat", "rat" ] }
{ "_id" : 2, "A" : [ "cat", "rat" ], "B" : [ ] }
{ "_id" : 3, "A" : [ ], "B" : [ "cat" ] }
{ "_id" : 4, "A" : [ "cat", "rat" ], "B" : [ "rat", "cat", "rat" ] }
{ "_id" : 5, "A" : [ "cat", "rat" ], "B" : [ "cat", "rat", "dog" ] }
{ "_id" : 6, "A" : [ "cat", "rat" ], "B" : [ [ "cat", "rat" ] ] }
{ "_id" : 7, "A" : [ "cat", "rat" ], "B" : [ [ "cat" ], [ "rat" ] ] }
{ "_id" : 8, "A" : [ ], "B" : [ ] }
{ "_id" : 9, "A" : [ "cat", "rat" ], "B" : [ "dog", "cat" ] }
}

Example : $setDifference

The following aggregation operation uses the $setDifference operator compares the array A and B to return an array of elements found in the B array but not in the A array:

> db.test_collection.aggregate(
...    [
...      { $project: { A: 1, B: 1, inBOnly: { $setDifference: [ "$B", "$A" ] }, _id: 0 } }
...    ]
... );

After the operation, following result will be returned by the $setDifference operator.

> db.test_collection.aggregate(
...    [
...      { $project: { A: 1, B: 1, inBOnly: { $setDifference: [ "$B", "$A" ] }, _id: 0 } }
...    ]
... );
{ "A" : [ "cat", "rat" ], "B" : [ "cat", "rat" ], "inBOnly" : [ ] }
{ "A" : [ "cat", "rat" ], "B" : [ ], "inBOnly" : [ ] }
{ "A" : [ ], "B" : [ "cat" ], "inBOnly" : [ "cat" ] }
{ "A" : [ "cat", "rat" ], "B" : [ "rat", "cat", "rat" ], "inBOnly" : [ ] }
{ "A" : [ "cat", "rat" ], "B" : [ "cat", "rat", "dog" ], "inBOnly" : [ "dog" ] }
{ "A" : [ "cat", "rat" ], "B" : [ [ "cat", "rat" ] ], "inBOnly" : [ [ "cat", "rat" ] ] }
{ "A" : [ "cat", "rat" ], "B" : [ [ "cat" ], [ "rat" ] ], "inBOnly" : [ [ "cat" ], [ "rat" ] ] }
{ "A" : [ ], "B" : [ ], "inBOnly" : [ ] }
{ "A" : [ "cat", "rat" ], "B" : [ "dog", "cat" ], "inBOnly" : [ "dog" ] }

Previous: $setUnion
Next: $setIsSubset



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb/aggregation/mongodb-aggregatrion-setdifference-operator.php