I have groups and students collections.
Groups: {_id : ObjectId, title: String}
Students{mainGroups: String}
mainGroups is concat string, that all groups, for every student has taken in school.
My code is giving me, all students collection for all groups.
I want to aggregate groups collection with students. And get students collection in one array, for every groups if how many students it has.
[
//mainGroups is String that`s why I convert Group`s $_id to String here
{$addFields: {
gid: {$toString:"$_id"}
}},
{$project: {
_id: 1,
gid: 1,
title:1
}},
{
$lookup: {
"from": 'students',
"let": {"groupId": "$gid"},
pipeline: [
{"$match":
{"$expr" :
{"mainGroups":{"$regex": "$$groupId", "$options" :"i"}}
}
}
],
as: "student"
}
},
]
How can I get how many students have for every groups ?