Reputation: 13
Let's say there's a user collection. The relevant information for this question from that collection is:
Now for the final output it should list the teams, list the users under their teams using the teamid, and adding up the totalscore of each user to calculate the teamscore.
However, I can't come up with any way to do that. I was looking into aggregation for this but eh, it's a bit too far above my hat.
The output for this api is rendered in json using express and mongoose.
Upvotes: 0
Views: 38
Reputation: 1579
You can use the following aggregation query.
Use a group
stage with $push
to get the player's name and $sum
to get the sum of totalscore
for a team.
db.coll.aggregate([
{'$group': {
'_id': '$teamid',
'totalscore': {'$sum': '$totalscore'},
'listofplayers': {'$push':'$username'}
}
}])
Upvotes: 1