I want to count the views of videos. I have the following structure.
{
_id: ObjectId("some_id")
views:[
{ userId:1, groupId:[1,2,4] },
{ userId:2, groupId:[2,4] },
{ userId:1, groupId:[3,5] }
]
}
I just need to count if both arrays (views.groupId and input array) have common elements, increase the count by one.
If the input array is is [1,3,5] then the no of views is 2. (first object has element 1 and third object has elements 3,5)
I wrote the following code, but it gives wrong answer.
{
$project: {
views: {
$size: {
$ifNull: [{
$filter: {
input: '$views',
as: 'views',
cond: {
$setIntersection: [
'$$views.groupId',
[1,3,5]
]
}
}
},
[]
]
}
}
}
}
I spent more time to solve it by myself, but no luck today. Thanks in advance