$setDifference
Returns an array containing elements that exist in the first set but not in the second set, performing set subtraction operation.
Syntax
{ $setDifference: [<array_expression1>, <array_expression2>] }
Parameters
array_expression1
objectrequiredThe first array expression (elements to keep)
array_expression2
objectrequiredThe second array expression (elements to subtract)
Examples
Find unique elements
Get elements that exist in first array but not in second
Query:
db.products.aggregate([{ $project: { uniqueTags: { $setDifference: ["$allTags", "$commonTags"] } } }])
Output:
Documents with uniqueTags containing tags not found in commonTags array