I have the following sample documents:
{
_id: ObjectId("61f3882cbd56c6d86dad92d9"),
charid: ObjectId("6140d7ca11c1853b3d42c1e6"),
pool: 'Stamina',
successes: 7
},
{
_id: ObjectId("61f576a7392b0461d801254a"),
charid: ObjectId("6140d7ca11c1853b3d42c1e6"),
pool: 'Composure',
successes: 1
},
{
_id: ObjectId("61f57a0fbb252e061c2a8227"),
charid: ObjectId("61f577e1bb252e061c2a820f"),
pool: 'Composure',
successes: 9
}
I need to group them all by charid
and add a traits
field that is an object with the sums of the pool
values, like so:
{
_id: ObjectId("6140d7ca11c1853b3d42c1e6"),
traits: {
Composure: 10,
Stamina: 7
}
}
How can I do this? I’ve looked at $mergeObjects
and $addFields
, but while either of those seem like they might work, I’m not exactly sure how to wrangle them into what I want.