title: Grouping in Spring Data MongoDB returns NULL _id when mapping to a class with composite key
body: <p>I want to aggregate a collection of documents that match certain conditions, group them and map the output to a different class object. The aggregation works fine and I get the expected <code>total</code> but the <code>_id</code> field is always NULL. </p>
  
  <p>I'm using spring-data-mongodb 2.1.11 and MongoDB 3.6.</p>
  
  <p>This is the class to be aggregated:</p>
  
  <pre><code>@Document
  public class LegOrder {
  
      public static class Key {
          @Indexed
          long itemId;
  
          long transactionId;
          ...
      }
  
      @Id
      private Key id;
  
      @Indexed
      private long brandId;
  
      private int units;
      ...
  }
  </code></pre>
  
  <p>This is the aggregation output class:</p>
  
  <pre><code>@Document
  public class ItemAggregation {
  
      public static class Key {
          @Indexed
          long itemId;
  
          @Indexed
          long brandId;
      }
  
      @Id
      private Key id;
  
      private long total;
      ...
  }
  </code></pre>
  
  <p>My aggregation method:</p>
  
  <pre><code>public ItemAggregation aggregate(long itemId, long brandId) {
      MatchOperation matchStage = Aggregation.match(new Criteria().andOperator(
              Criteria.where("id.itemId").is(itemId),
              Criteria.where("brandId").is(brandId)
      ));
  
      GroupOperation groupStage = Aggregation.group("id.itemId", "brandId")
              .sum("units").as("total")
              ...
              ;
  
      Aggregation aggregation = Aggregation.newAggregation(matchStage, groupStage);
  
      return mongoTemplate.aggregate(aggregation, LegOrder.class, ItemAggregation.class).getUniqueMappedResult();
  }
  </code></pre>
  
  <p>The executed query in MongoDB:</p>
  
  <pre><code>[
    {
      "$match": {
        "$and": [
          { "_id.itemId": 1 },
          { "brandId": 2}
          ]
      }
    },
    {
      "$group": {
        "_id": {
          "itemId": "$_id.itemId",
          "brandId": "$brandId"
        },
        "total": { "$sum": "$units" }
      }
    }
  ]
  </code></pre>
  
  <p>If I run this query in the mongo shell the <code>_id</code> field is properly populated.</p>
  
  <p>Any idea how to achieve it?</p>
  
  <p>Thank you</p>