title: MongoDB find and return all with max value
body: <p>I'm trying to get a list of documents that have the max value. I can specify it if I know the number of documents with max value (like in the solution in this another stackoverflow solution 'mongodb how to get max value from collections'), but I don't know how to do it if I don't know what the number of documents is. </p>
  
  <p>For example, using the following documents:</p>
  
  <pre><code>{name:"a", age:10}
  {name:"b", age:11}
  {name:"d", age:12}
  {name:"c", age:12}
  </code></pre>
  
  <p>So I know that there are 2 documents with max age of 12. Therefore I can write the following query</p>
  
  <pre><code>db.collection.find().sort({age: -1).limit(2)
  </code></pre>
  
  <p>I use <code>limit(2)</code> because I know that there are 2 documents with a max value, but how can I automate that? Can I count the records with max value, store it in a variable and use it like <code>limit(n)</code>? Is there any other way to do it?</p>