title: Java MongoDB aggregation - sum of multiple fields
body: <p>I am trying to perform a mongodb aggregation using Java and unable to figure out how to sum 2 fields individually.  Below is my document structure:</p>
  
  <pre><code>{
      "_id" : ObjectId("59b6b96423b65d0a04de128d"),
      "itemCount": 25,
      "defectiveItemCount": 5,
      "time": ISODate("x")
  },
  {
      "_id" : ObjectId("59b6b96423b65d0a04de128d"),
      "itemCount": 20,
      "defectiveItemCount": 7,
      "time": ISODate("x")
  }
  </code></pre>
  
  <p>I am trying to sum using:</p>
  
  <pre><code>Aggregation pipeline = newAggregation(
          match(Criteria.where("time").gt(time)),
          group().sum("itemCount").as("total"),
          group().sum("defectiveItemCount").as("defective"),
          project("total").and("defective").previousOperation()
  );
  </code></pre>
  
  <p>I also tried:</p>
  
  <pre><code>Aggregation pipeline = newAggregation(
          match(Criteria.where("time").gt(time)),
          group().sum("itemCount").as("total").sum("defectiveItemCount").as("defective"),
          project("total").and("defective").previousOperation()
  );
  </code></pre>
  
  <p>When I run this aggregation,  the result of defective is always NULL.  </p>
  
  <p>I want the result as:</p>
  
  <pre><code>{
      "itemCount": 45,
      "defectiveItemCount": 12
  }
  </code></pre>
  
  <p>Is the above possible or should I perform 2 separate aggregations?</p>