title: Spring Data - MongoDB - Aggregation how to project all fields of grouped reference document
body: <p>I have 2 documents, Topic and comment. Each topic has many comments and the documents look like this:</p>
  
  <pre><code>@Document
  public class Topic {
      @Id
      private String id;
  
      @Indexed(unique = true)
      @NotBlank
      private String title;
  }
  
  
  @Document
  public class Comment {
  
      @NotBlank
      private String text;
  
      @Indexed
      @NotBlank
      private String topic;  // id of topic
  
      @CreatedDate
      @Indexed
      private LocalDateTime createdDate;
  }
  </code></pre>
  
  <p>So I actually save the id reference of Topic within comments.</p>
  
  <p>This is my aggregation scenario: <strong>List the topics, which received most comments today</strong>. So three things:</p>
  
  <ul>
  <li>Get all comments of today (MatchOperation)</li>
  <li>Group by topic and sum comments (GroupOperation)</li>
  <li>Sort by this sum (SortOperation)</li>
  </ul>
  
  <p>This is the code so far:</p>
  
  <pre><code>MatchOperation filterCreatedDate = match(Criteria.where("createdDate").gte(today.atStartOfDay()).lt(today.plusDays(1).atStartOfDay()));
  GroupOperation groupByTopic = group("topic").count().as("todaysCommentsCount");
  SortOperation sortByTodaysCommentsCount = sort(Sort.Direction.DESC, "todaysCommentsCount");
  
  Aggregation aggregation = newAggregation(filterCreatedDate, groupByTopic, sortByTodaysCommentsCount);
  AggregationResults&lt;TopTopic&gt; result = mongoTemplate.aggregate(aggregation, Comment.class, TopTopic.class);
  </code></pre>
  
  <p>This is a special Class for this output:</p>
  
  <pre><code>public class TopTopic {
      private int todaysCommentsCount;
  }
  </code></pre>
  
  <p>And this is the output of my aggregation where there is only one topic:</p>
  
  <pre><code>{
      "mappedResults": [
          {
              "todaysCommentsCount": 3
          }
      ],
      "rawResults": {
          "results": [
              {
                  "_id": "5dbdca8112a617031728c417",     // topic id
                  "todaysCommentsCount": 3
              }
          ],
          "ok": 1.0
      },
      "serverUsed": null,
      "uniqueMappedResult": {
          "todaysCommentsCount": 1
      }
  }
  </code></pre>
  
  <p>I thought I am actually pretty close, but somehow it works only when I have only one topic. When there are comments from more than one topic which should then create multiple groups, I get this error:</p>
  
  <pre><code>Could not write JSON: Expected unique result or null, but got more than one!; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Expected unique result or null, but got more than one! (through reference chain: org.springframework.data.mongodb.core.aggregation.AggregationResults[\"uniqueMappedResult\"]
  </code></pre>
  
  <p>.. althought i do not call any getUniqueMappedResult method.</p>
  
  <p>What do I do wrong?</p>
  
  <p>Secondly, how can I get rid of my output class TopTopic, and instead return the original Topic values extended with todaysCommentsCount, without creating a special output class?</p>
  
  <p>I appreciate for any help.</p>