How to do row_number partition by in mongo
<p>I have a Sql like:</p>

<pre><code>SELECT *
FROM (
   SELECT Row_number() OVER(PARTITION BY bpid ORDER BY LoginTime DESC) rn, userid
   FROM xxx_table) t
WHERE rn &lt;= 3
</code></pre>

<p>How to do this in mongodb? I write query like this:</p>

<pre><code>{$sort:{"LatestLoginTime":-1}},
{"$limit":2000},
{
    $group:{
        _id:"$BPID",
        items:{
            $push:{
                userId:'$UserID',
                date:'$LatestLoginTime'
            }
        }
    }
}
</code></pre>