$setUnion
Returns an array containing all distinct elements from the input arrays, performing set union operation to combine arrays without duplicates.
Syntax
{ $setUnion: [<array_expression1>, <array_expression2>, ...] }
Parameters
array_expressions
objectrequiredTwo or more array expressions to combine into union set
Examples
Combine arrays
Create union of two arrays with distinct elements
Query:
db.products.aggregate([{ $project: { allTags: { $setUnion: ["$productTags", "$categoryTags"] } } }])
Output:
Documents with allTags containing distinct elements from both tag arrays