MongoDB Query, sort then take nth document for group
<p>I am trying to compose a <a href="https://docs.mongodb.com/v3.0/reference/operator/aggregation/first/" rel="noreferrer">MongoDB</a> query...</p>

<p>My aim is to retrieve a list of player records by <code>country</code> sorted by <code>rating</code> and then return <strong>only</strong> the nth rated player for each country (so the top rated, or the 3rd best rated etc).</p>

<p>I have achieved part I: </p>

<pre><code>db.getCollection('players').find( { event: 'open' }).sort({ country: 1, rating: -1 });
</code></pre>

<h3>Update</h3>

<p>Here is sample of two countries with three players for each, ordered by rating:</p>

<p><strong>Team One:</strong></p>

<pre><code>{
    "id" : 400041,
    "name" : "Adams Michael",
    "rating" : 2727,
    "country" : "England",
    "event" : "open"
},
{
    "id" : 404853,
    "name" : "McShane Luke J",
    "rating" : 2671,
    "country" : "England",
    "event" : "open",
},
{
    "id" : 400025,
    "name" : "Short Nigel D",
    "rating" : 2666,
    "country" : "England",
    "event" : "open"
}
</code></pre>

<p><strong>Team Two:</strong></p>

<pre><code> {
    "id": 4101588,
    "name": "Kramnik Vladimir",
    "rating": 2808,
    "country": "Russia",
    "event": "open"
},
{
    "id": 4126025,
    "name": "Grischuk Alexander",
    "rating": 2784,
    "country": "Russia",
    "event": "open"
},
{
    "id": 14109603,
    "name": "Karjakin Sergey",
    "rating": 2769,
    "country": "Russia",
    "event": "open"
}
</code></pre>

<p>I want my query to return the objects for: </p>

<ul>
<li>Adams Michael</li>
<li>Kramnik Vladimir</li>
</ul>

<p>(as these are the two highest rated) </p>

<p>I want my query to also allow me to query for the second highest rated:</p>

<ul>
<li>McShane Luke J</li>
<li>Grischuk Alexander</li>
</ul>