title: How to get ranking position of a mongoDB collection?
body: <p>I have a mongoDB collection that looks like this:</p>
  <pre><code>{
      &quot;_id&quot;: 1,
      &quot;name&quot;: &quot;John Doe&quot;,
      &quot;company&quot;: &quot;Acme&quot;,
      &quot;email&quot;: &quot;john.doe@acme.com&quot;,
      &quot;matches&quot;: [171844, 169729, 173168, 174310, 168752, 174972, 172959, 169546]
  }
  {
      &quot;_id&quot;: 2,
      &quot;name&quot;: &quot;Bruce Wayne&quot;,
      &quot;company&quot;: &quot;Wayne Enterprises&quot;,
      &quot;email&quot;: &quot;bruce@wayne.com&quot;,
      &quot;matches&quot;: [171844, 232333, 233312, 123456]
  }
  {
      &quot;_id&quot;: 3,
      &quot;name&quot;: &quot;Tony Stark&quot;,
      &quot;company&quot;: &quot;Stark Industries&quot;,
      &quot;email&quot;: &quot;tony@stark.com&quot;,
      &quot;matches&quot;: [173844, 155729, 133168, 199310, 132752, 139972]
  }
  {
      &quot;_id&quot;: 4,
      &quot;name&quot;: &quot;Clark Kent&quot;,
      &quot;company&quot;: &quot;Daily Planet&quot;,
      &quot;email&quot;: &quot;clark.kent@planet.com&quot;,
      &quot;matches&quot;: [169729, 174310, 168752]
  }
  {
      &quot;_id&quot;: 5,
      &quot;name&quot;: &quot;Lois Lane&quot;,
      &quot;company&quot;: &quot;Daily Planet&quot;,
      &quot;email&quot;: &quot;lois.lane@planet.com&quot;,
      &quot;matches&quot;: [172959, 169546]
  }
  </code></pre>
  <p>I need to get a filtered list of users but with a key that shows the user's &quot;ranking&quot; position based on the number of &quot;matches&quot; records that it has.
  There should be a <code>global ranking</code> position and a <code>company ranking</code> position.</p>
  <p>The desired result should be this (for an example filtering for company='Daily Planet'):</p>
  <pre><code>{
      _id: 4,
      name: 'Clark Kent',
      company: 'Daily Planet',
      email: 'clark.kent@planet.com',
      points: 3,     // &lt;=
      globalRank: 4, // &lt;=
      companyRank: 1 // &lt;=
  },
  {
      _id: 5,
      name: 'Lois Lane',
      company: 'Daily Planet',
      email: 'lois.lane@planet.com',
      points: 2,     // &lt;=
      globalRank: 4, // &lt;=
      companyRank: 2 // &lt;=
  }
  </code></pre>
  <p>Notice that Clark Kent is ranked 4th on the global ranking since he has 3 matches (John Doe, Bruce Wayne and Tony Stark have more matches than him) and is ranked 1st on the company Ranking, since he has more matches than any Daily Planet user.</p>
  <p>However, even after several days of research, I couldn't find a way to do it. (I couldn't even figure out how to do the global ranking or company ranking ONLY).</p>
  <p>Any ideas on how to solve this, or on how to approach the problem in a different way?</p>