$setIntersection
The $setIntersection operator in Amazon DocumentDB is used to return the common elements between two or more arrays. This operator is particularly useful when working with sets of data, allowing you to find the intersection of multiple sets.
Parameters
-
array1: The first array to intersect. -
array2: The second array to intersect. -
arrayN: (optional) Additional arrays to intersect.
Example (MongoDB Shell)
The following example demonstrates how to use the $setIntersection operator to find the common elements between two arrays.
Create sample documents
db.collection.insertMany([ { _id: 1, colors: ["red", "blue", "green"] }, { _id: 2, colors: ["blue", "yellow", "orange"] }, { _id: 3, colors: ["red", "green", "purple"] } ])
Query example
db.collection.aggregate([ { $project: { _id: 1, commonColors: { $setIntersection: ["$colors", ["red", "blue", "green"]] } } } ])
Output
[
{ "_id": 1, "commonColors": ["red", "blue", "green"] },
{ "_id": 2, "commonColors": ["blue"] },
{ "_id": 3, "commonColors": ["red", "green"] }
]
Code examples
To view a code example for using the $setIntersection command, choose the tab for the language that you want to use: