Retain document field through Aggregation Pipeline

I have the following aggregation pipeline, but I would like to have the hcapPlaying attribute in the final outputted document as a named attribute.

[{$match: {
  'compId': ObjectId('6056fe9cb36a4f58ff44055d')
}}, {$group: {
  _id: [
  'score': {
    '$sum': '$computedScore'
  'thru': {
    '$sum': '$computedThru'
}}, {$addFields: {
  'hcap': '$_id.hcapPlaying'
}}, {$sort: {
  'score': 1

But in the syntax above, the new hcap attribute is an empty Array.

instead of this:

try this:

{$addFields: {
  'hcap': {
    $arrayElemAt: ['$_id', 2]

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.