If I have Player
documents of the form
{name: String, score: Int}
and I have Group
documents, where groups represent lists of players
{groupName: String, players: [ObjectID]}
Players can belong to multiple groups.
I want to do an aggregation of Player
documents, grouped by Group
(ex. get the sums of players' scores for each group, with one aggregation pipeline).
Options I'm aware of:
=> give Player
docs back pointers to Group
documents they are a associated with, then $group
by GroupID
. But I prefer to not have to modify the Player
collection. (perhaps there is a way to "add in" GroupID
s to the docs during the pipeline?)
=> make a separate call for each group and use a $match
stage to filter to just players in the current group being queried for. But I prefer to make a single, simple call.
How can I achieve such an aggregation? Does MongoDB Aggregation have something that works for this?
(By the way, it's not a problem to have the groups and players, mapped to each other, in memory where the call is being made. So options involving passing lists of players as arguments are indeed fair game.)