- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $setIntersection (aggregation)
$setIntersection (aggregation)¶
On this page
Definition¶
-
$setIntersection
¶ Takes two or more arrays and returns an array that contains the elements that appear in every input array.
$setIntersection
has the following syntax:The arguments can be any valid expression as long as they each resolve to an array. For more information on expressions, see Expressions.
Behavior¶
$setIntersection
performs set operation on arrays, treating arrays
as sets. If an array contains duplicate entries, $setIntersection
ignores the duplicate entries. $setIntersection
ignores the order of
the elements.
$setIntersection
filters out duplicates in its result to output an
array that contain only unique entries. The order of the elements in
the output array is unspecified.
If a set contains a nested array element, $setIntersection
does not descend
into the nested array but evaluates the array at top-level.
Example | Result |
---|---|
Example¶
Consider an experiments
collection with the following documents:
The following operation uses the $setIntersection
operator to return an array of elements common to both the A
array and the B
array:
The operation returns the following results: