title: Spring MongoDB Aggregation Group - Can't get the group query right
body: <p>I have a document in a MongoDB, which looks like follows.</p>
  
  <pre><code>{
      "_id" : ObjectId("5ceb812b3ec6d22cb94c82ca"),
      "key" : "KEYCODE001",
      "values" : [ 
          {
              "classId" : "CLASS_01",
              "objects" : [ 
                  {
                      "code" : "DD0001"
                  }, 
                  {
                      "code" : "DD0010"
                  }
              ]
          }, 
          {
              "classId" : "CLASS_02",
              "objects" : [ 
                  {
                      "code" : "AD0001"
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>I am interested in getting a result like follows.</p>
  
  <pre><code>{
      "classId" : "CLASS_01",
      "objects" : [ 
          {
              "code" : "DD0001"
          }, 
          {
              "code" : "DD0010"
          }
      ]
  }
  </code></pre>
  
  <p>To get this, I came up with an aggregation pipeline in Robo 3T, which looks like follows. And it's working as expected.</p>
  
  <pre><code>[
      {
          $match:{
              'key':'KEYCODE001'
          }
      },
      {
          "$unwind":{
              "path": "$values",
              "preserveNullAndEmptyArrays": true
          }
      },
      {
          "$unwind":{
              "path": "$values.objects",
              "preserveNullAndEmptyArrays": true
          }
      },
      {
          $match:{
              'values.classId':'CLASS_01'
          }
      },
      {
          $project:{
              'object':'$values.objects',
              'classId':'$values.classId'
          }
      },
      {
          $group:{
              '_id':'$classId',
              'objects':{
                  $push:'$object'
              }
          }
      },
      {
          $project:{
              '_id':0,
              'classId':'$_id',
              'objects':'$$objects'
          }
      }
  ]
  </code></pre>
  
  <p>Now, when I try to do the same in a SpringBoot application, I can't get it running. I ended up having the error <code>java.lang.IllegalArgumentException: Invalid reference '$complication'!</code>. Following is what I have done in Java so far.</p>
  
  <pre><code>final Aggregation aggregation = newAggregation(
          match(Criteria.where("key").is("KEYCODE001")),
          unwind("$values", true),
          unwind("$values.objects", true),
          match(Criteria.where("classId").is("CLASS_01")),
          project().and("$values.classId").as("classId").and("$values.objects").as("object"),
          group("classId", "objects").push("$object").as("objects").first("$classId").as("_id"),
          project().and("$_id").as("classId").and("$objects").as("objects")
  );
  </code></pre>
  
  <p>What am I doing wrong? Upon research, I found that multiple fields in group does not work or something like that (please refer to <a href="https://stackoverflow.com/questions/18653574/spring-data-mongodb-aggregation-framework-invalid-reference-in-group-operati">this question</a>). So, is what I am currently doing even possible in Spring Boot?</p>