title: Spring Data MongoDB Aggregation involve Unwind,Match,Sort &Group
body: <p>I have a collection named <strong>User</strong> which contains embedded document called <strong>Message</strong>. The embedded document <strong>Message</strong> contains array of <strong>message object</strong> as below :</p>
  
  <pre><code>{
    "_id" : ObjectId("58e09daa192216e39fd85433"),
    "userId" : "user123",
    "message" : [ 
      {
          "messageId" : "5277941e-9d84-46c3-b927-ef33abbf35f2",
          "dateCreated" : 1491115000,
          "body" : "howdy?",
          "type" : "text"
      }, 
      {
          "messageId" : "c2ce0480-bc0d-4393-89d4-27174d323b98",
          "dateCreated" : 1491119000,
          "body" : "i've problem with my account. can you help?",
          "type" : "text"
      }, 
      {
          "messageId" : "45b2593c-a960-4066-8723-db2531dd8bab",
          "dateCreated" : 1491100000,
          "body" : "this is urgent",
          "type" : "text"
      }
    ]
  }
  </code></pre>
  
  <p>My objective is to sort embedded document <strong>Message</strong> based on <strong>dateCreated</strong> key. I need to translate this mongo query into Spring Data MongoDB :</p>
  
  <pre><code>db.user.aggregate(
    {$unwind: "$message"}, 
    {$match: {userId: "user123"}},
    {$sort: {"message.dateCreated": 1}},
    {$group: {_id: "$_id", "message": {"$push": "$message"}}})
  </code></pre>
  
  <p>I've tried the following codes but still getting error :</p>
  
  <pre><code>AggregationOperation unwind = Aggregation.unwind("message");
  AggregationOperation match = Aggregation.match(Criteria.where("userId").in("user123"));
  AggregationOperation sort = Aggregation.sort(Direction.ASC, "message.dateCreated");
  AggregationOperation group = Aggregation.group("userId", "message");
  
  Aggregation aggregation = Aggregation.newAggregation(unwind, match, sort, group);
  
  AggregationResults&lt;User&gt; groupResults = mongoTemplate.aggregate(aggregation, User.class, User.class);
  </code></pre>
  
  <p>Error :</p>
  
  <blockquote>
    <p>Failed to instantiate [java.util.List]: Specified class is an interface</p>
  </blockquote>
  
  <p>User class :</p>
  
  <pre><code>@Document(collection="user")
  public class User {
  
    @Id
    private String id;
    private String userId;
    @Field("message")
    @DBRef
    private List&lt;Message&gt; message;
    //constructor, getter, setter
  }
  </code></pre>
  
  <p>Message class :</p>
  
  <pre><code>@Document
  public class Message {
    private String messageId;
    private long dateCreated;
    private String body;
    private String type;
    //constructor, getter, setter
  }
  </code></pre>
  
  <p>Hint :</p>
  
  <p>Based on my research, I'm pretty sure that I need to use <strong>GroupOperationBuilder group = 
   Aggregation.group("userId").push("message")</strong> but I don't really know how to proceed with this as AggregationResults will not allow me to use it in the <strong>mongoTemplate.aggregate()</strong></p>