Unwind , Group and still keep original data

Hi @slava and the rest. I got the previous problem to work. Now the new struggle I have is i want to group my data on area but still retain all the other values that are contained in each array element. I tried this.

db.Slava1.aggregate([
 {
$project: {
  'teamStats.data': {
    $map: {
      input: '$teamStats.data',
      in: {
        $let: {
          vars: {
            newArea: {
              $substr: ['$$this.area', 0, 1],
            },
          },
          in: {
            $mergeObjects: [
              '$$this',
              {
                area: '$$newArea',
              },
            ],
          },
        },
      },
    },
  },
},
 },
{
  $unwind: '$teamStats.data',
 },
 {
  $group: {
   _id: {
     Game: '$_id',
    Area: '$teamStats.data.area',
   },
},
},

{
 $project: {
  _id: 0,
  Game: '$_id.Game',
  Area: '$_id.Area',
},
},
]);

and then i get this result.
mongo3

So i want it to be grouped like that with the game name but i still want all the other data also retained in each array object.

Hello, @johan_potgieter! Sorry of a big delay - work does not wait :slight_smile:

In order to help you, provide:

  • prettified example of one document from your collection
  • prettified exampe of desired result