title: MongoDB aggregate, how to addToSet each element of array in group pipeline
body: <p>I have documents that contains a tags fields. It's a simple array with tag names inside, no object nor _id inside.
  Just plain tags like this <code>["Protocol", "Access", "Leverage", "Capability"]</code>.</p>
  
  <p>And in my group pipeline I tried something like <code>'selectedTags': { $addToSet: '$tags' }</code> but then I end up with an array containing arrays of tags. And I get the same with <code>$push</code>.</p>
  
  <p>I tried to use <code>$each</code> or <code>$pushAll</code> but they are not supported as grouping operator as my shell tell me.</p>
  
  <p>Can someone help me on this one please ?</p>
  
  <p>Thank you</p>
  
  <p>Edit:</p>
  
  <p>Sample docs:</p>
  
  <pre><code>{
      "_id" : "HWEdDGsq86x4ikDSQ",
      "teamId" : "AdLizGnPuqbWNsFHe",
      "ownerId" : "Qb5EigWjqn2t3bfxD",
      "type" : "meeting",
      "topic" : "Grass-roots hybrid knowledge user",
      "fullname" : "Guidouil",
      "startDate" : ISODate("2017-07-30T09:00:05.513Z"),
      "shareResults" : true,
      "open" : true,
      "language" : "fr",
      "tags" : [
          "Protocol",
          "Challenge",
          "Artificial Intelligence",
          "Capability"
      ],
      "isDemo" : true,
      "createdAt" : ISODate("2017-11-15T19:24:05.513Z"),
      "participantsCount" : 10,
      "ratersCount" : 10,
      "averageRating" : 3.4,
      "hasAnswers" : true,
      "updatedAt" : ISODate("2017-11-15T19:24:05.562Z")
  }
  {
      "_id" : "rXvkFndpXwJ6KAvNo",
      "teamId" : "AdLizGnPuqbWNsFHe",
      "ownerId" : "Qb5EigWjqn2t3bfxD",
      "type" : "meeting",
      "topic" : "Profit-focused modular system engine",
      "fullname" : "Guidouil",
      "startDate" : ISODate("2017-07-24T12:00:05.564Z"),
      "shareResults" : true,
      "open" : true,
      "language" : "fr",
      "tags" : [
          "Initiative",
          "Artificial Intelligence",
          "Protocol",
          "Utilisation"
      ],
      "isDemo" : true,
      "createdAt" : ISODate("2017-11-15T19:24:05.564Z"),
      "participantsCount" : 33,
      "ratersCount" : 33,
      "averageRating" : 2.9393939393939394,
      "hasAnswers" : true,
      "updatedAt" : ISODate("2017-11-15T19:24:05.753Z")
  }
  </code></pre>
  
  <p>Aggregation:</p>
  
  <pre><code>db.surveys.aggregate(
    { $match: query },
    {
      $group: {
        '_id': {
          'year': { $year: '$startDate' },
          'day': { $dayOfYear: '$startDate' },
        },
        'participants': { $sum: '$ratersCount' },
        'rating': { $avg: '$averageRating' },
        'surveys': { $push: '$_id' },
        'selectedTags': { $addToSet: '$tags' },
        'peoples': { $addToSet: '$fullname' },
      }
    },
    { $sort: { _id: 1 } }
  );
  </code></pre>
  
  <p>then I tried to change the selectedTags to <code>{ $push: { $each: '$tags' } }</code> or <code>{ $pushAll: '$tags' }</code> but this does not execute :(</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>In javascript I do it like that:</p>
  
  <pre><code>return Surveys.aggregate(
    { $match: query },
    { $group: {
      _id: dateGroup,
      participants: { $sum: '$ratersCount' },
      rating: { $avg: '$averageRating' },
      surveys: { $push: '$_id' },
      selectedTags: { $push: '$tags' },
      peoples: { $addToSet: '$fullname' },
    } },
    { $project: {
      _id: null,
      selectedTags: {
        $reduce: {
          input: "$selectedTags",
          initialValue: [],
          in: { $setUnion: ["$$value", "$$this"] }
        }
      },
    } }
  );
  </code></pre>