title: How to Find MongoDB Documents with the Same Value and Count Them?
body: <p>In MongoDB, I need to use group aggregation (I believe), in order to get the number of documents in a collection with the same value. I need to get these results returned to me from greatest to least, and then get the common value for each result.</p>
  
  <p>Eg.</p>
  
  <ol>
  <li><p>I have a normal query with a range (eg. field "value" > 5). I assume for this I should use the "match" feature when aggregating</p></li>
  <li><p>Get all documents with the same value for the "id" field, that also match the above query parameters</p></li>
  <li><p>Sort the results from most matching values to least</p></li>
  <li><p>Give me the common value of "id" for each result.</p></li>
  </ol>
  
  <p>Sample documents:</p>
  
  <pre><code>    #1. Type: "Like", value: 6, id: 123
      #2. Type: "Like", value: 7, id: 123
      #3. Type: "Like", value: 7, id: 123
      #4. Type: "Like", value: 8, id: 12345
      #5. Type: "Like", value: 7, id: 12345
      #6. Type: "Like", value: 6, id: 1234
      #7. Type: "Like", value: 2, id: 1234
      #7. Type: "Like", value: 2, id: 1234
      #7. Type: "Like", value: 2, id: 1234
  </code></pre>
  
  <p>Expected output (assume I have a limit of 3 documents, and the query asks for only documents with the "value" field > 5):</p>
  
  <pre><code>1. id: 123
  2. id: 12345
  3. id: 1234
  </code></pre>
  
  <p>I expect these in this order, as the id 123 is most popular, and 1234 is least popular, of the documents where the "value" field > 5.</p>
  
  <p>Ideally, I would have a method that would return something like a String[] of the resulting Ids, in order.</p>