title: Translate mongodb aggregate query into Java/Kotlin Spring Data
body: <p>So I came up with a query that I'm not really sure how to translate into Spring Data. This is the query:</p>
  <pre><code>db.collection.aggregate([{
      {
          $group: {
              _id: &quot;$field&quot;,
              count: {
                  $sum: &quot;$count&quot;
              },
              data: {
                  &quot;$addToSet&quot;: &quot;$$ROOT&quot;
              }
          }
      },
      {
          $sort: {
              count: -1
          }
      },
      {
          $limit: 10
      },
      {
          $unwind: &quot;$data&quot;
      }
  ])
  </code></pre>
  <p>The problematic part is <code>unwind</code>. In this raw query I'm putting the original document(&quot;$$ROOT&quot;) that is being processed into <code>data</code>, so after the set is being processed I can just unwind it and end up with just the documents that I really want. This works fine from the shell. The problem is that I'm not seeing how to do the same operation in Java/Kotlin, I can just add to the set without having a way to reference that data later. Could someone help me out and write how this would look in Java/Kotlin with Spring Data?</p>