title: mongoDB filter, sort, and rank result
body: <p>I have a collection like this</p>
  <pre><code>{
   id: 1,
   category: &quot;food&quot;,
   score: 20
  }
  {
   id: 2,
   category: &quot;drink&quot;,
   score: 19
  }
  {
   id: 3,
   category: &quot;food&quot;,
   score: 50
  }
  {
   id: 4,
   category: &quot;food&quot;,
   score: 30
  }
  </code></pre>
  <p>id is not unique btw.
  but it is unique in that category.</p>
  <p>so it is possible to have</p>
  <pre><code>{id: 1, category: &quot;food&quot;}
  {id: 1, category: &quot;drink&quot;}
  </code></pre>
  <p>but not</p>
  <pre><code>{id: 1, category: &quot;food&quot;}
  {id: 1, category: &quot;food&quot;}
  </code></pre>
  <pre><code>here's what I want to do
  
  find all category == &quot;food&quot;
  -&gt; it will give id: 1, 3, 4
  // I can add some other filter here before sort happen
  // like id less than 100
  
  sort them by score
  -&gt; it will give id: 3, 4, 1 // highest score must be the first entry
  
  then what is the rank of id: [4, 1]
  -&gt; it should give me {id: 4, rank: 2}, {id: 1, rank: 3}
  </code></pre>
  <p>how can I achieve this?
  please give me some snippets or idea</p>