title: How do I use Spring Mongo to group two fields and get a array of one fields based on the other?
body: <p>Let me give a example here:</p>
  
  <p>Two entries in the collection Author:</p>
  
  <pre><code>{
  "name" : "Joe"
  "Book" : "A"
  },
  {
  "name" : "Joe"
  "Book" : "B"
  }
  </code></pre>
  
  <p>Now, if I use the aggregation function in Mongo via spring mongo, basically just to grab the books with name Joe, it could be coded like:</p>
  
  <pre><code>Aggregation agg = newAggregation(Map.class, group("name", "Book"));
  
  AggregationResults&lt;Map&gt; results = mongoTemplate.aggregate(agg, "Author",
                  Map.class);
  </code></pre>
  
  <p>Obviously I could get two Maps this way, one has entry <code>{"name":"Joe", "Book": A}</code>, the other has <code>{"name" : "Joe", "Book" : "B"}</code></p>
  
  <p>But what if I want get ONLY one result back, with one entry : </p>
  
  <pre><code>{"name" : Joe, "Books" : ["A", "B"]}?
  </code></pre>
  
  <p>I'm not sure if it is reachable just using one query. It certainly could be achieved by multiple steps, which I'd hate to do.. </p>