Reputation: 11
I have to do some aggregation and I have to count into a new key value, the number of elements from an array that appears in said document, for example, if i had a list of actors, the answer would be the number of actors from this list that appears in one movie. could be all of them or just one.
I'm not sure if i should be using the $count or what.
Upvotes: 1
Views: 193
Reputation: 12817
You should need to find the intersection and get the size of the array.
$setIntersection
will accept arrays as input and return the matching elements in an array,so one array would be your search array and another would be the one from the document, once get the intersection get count using $size
db.movies.aggregate(
[
{
$project: {
actorsCount: { $size: { $setIntersection : [$searchArray, $actor] } }
}
}
]
)
Upvotes: 1