0

Say I have the following documents that represent a log of each character's dice throws in an RPG:

{ character: 'Bill', outcome: 'success', ... },
{ character: 'Jim', outcome: 'fail', ... },
{ character: 'Veronica', outcome: 'critical', ... },
...

Using an aggregation pipeline, I want to generate lifetime statistics of each character's roll outcomes:

[{
    'character': 'Bill',
    'outcomes': [
        'critical': 8,
        'success': 32,
        'failure': 20,
        ...
    ]
},
{
    'character': 'Veronica',
    ...
}
]

The $sortByCount stage would seem to be ideal; however, I can't find a way to group its results by any particular criteria; it matches against all documents. I know how I could manually match the outcome types (there are six total) and count them per character, but I'm wondering if there's a succinct way of doing so like $sortByCount. Something like:

db.characters.aggregate([
{
  $lookup: {
    from: 'roll_stats',
    localField: '_id',
    foreignField: 'charid',
    as: 'roll'
  }
},
{
  $unwind: '$roll'
},
{
  $group: {
    _id: { _id: '$_id', name: '$name' },
    outcomes: { $sortByCount: '$roll.outcome' }
  }
},
{
  $project: {
    _id: 0,
    name: '$_id.name',
    outcomes: 1
  }
}
])

Only, $sortByCount can't be used in the $group aggregation stage.

What's the best way to accomplish this?

tiltowait
  • 100
  • 1
  • 7
  • The [$sortByCount](https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/) is a pipeline stage level operator, and it is a combination of `$group` and `$sort` stages. – turivishal Oct 05 '21 at 16:11
  • This might help: https://stackoverflow.com/questions/15388127/mongodb-sort-inner-array – Joe Oct 06 '21 at 04:37

0 Answers0