title: Group Elements after unwind and ignore duplication - MongoDB Aggregation
body: <p>I use aggregation to unwind an array, and group by each element of array to sum some correspondant values.</p>
  
  <p>My collection is like :</p>
  
  <pre><code>/* 1 */
  {
      "_id" : ObjectId("59ce411c2708c97154d1319b"),
      "sourceMediumPath" : [ 
          {
              "nodeValue" : "(direct) / (none)"
          }, 
          {
              "nodeValue" : "(direct) / (none)"
          }
      ],
      "totalConversions" : 1,
      "totalConversionValue" : 171.6,
  }
  
  /* 2 */
  {
      "_id" : ObjectId("59ce411c2708c97154d136a0"),
      "sourceMediumPath" : [ 
          {
              "nodeValue" : "google / cpc"
          }, 
          {
              "nodeValue" : "(direct) / (none)"
          }, 
          {
              "nodeValue" : "google / cpc"
          }
      ],
      "totalConversions" : 1,
      "totalConversionValue" : 151.8,
  }
  </code></pre>
  
  <p>I want to group by sourceMedium.nodeValue and sum 'totalConverions' and 'totalConversionValue' without conseidering the duplicate elements.</p>
  
  <p>For example by using the unwinding, grouping, and sum :</p>
  
  <pre><code>aggregation = Aggregation.newAggregation(
                      Aggregation.unwind("sourceMediumPath"),
                      Aggregation.group("sourceMediumPath.nodeValue")
                              .sum("totalConversions").as(Variables.TOTAL_CONNVERSIONS)
                              .sum("TotalConversionValue").as(Variables.TOTAL_CONVERSION_VALUE),
  
                      Aggregation.project("sourceMediumPath.nodeValue")
                              .andInclude(Variables.TOTAL_CONNVERSIONS, Variables.TOTAL_CONVERSION_VALUE)
  </code></pre>
  
  <p>I get for "nodeValue" : "(direct) / (none)" a Sum of TotalConversions equals to 3 and for "google / cpc" a Sum equals to 1. Because it duplicate totalConversions and totalConversions while doing the unwind operation.</p>
  
  <p>There is any solution to ignore the duplication and have just one value per document.</p>
  
  <p>how can I do that ? </p>