w3resource

MongoDB Aggregation Set Operator - $setUnion

MongoDB: $setUnion

The MongoDB $setUnion operators take any number of argument expressions or arrays and return an array containing the elements that appear in any input array.

Syntax:

{ $setUnion: [ <expression1>, <expression2>, ... ] }
  • The $setUnion operator can only performs set operation on arrays and treated as sets. If any duplicate value contains in an array, it ignores by $setUnion 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 $setUnion operation.
  • When a set contains a nested array element, the $setUnion 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: $setUnion

The following aggregation operation uses the $setUnion operator compares the array A and B returns an array set that contain the common elements as well as the rest elements found in both the arrays.

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

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

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

Previous: $setIntersection
Next: $setDifference



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-setunion-operator.php