我有一个 mongoDB 集合,如下所示:
{
"_id": 1,
"name": "John Doe",
"company": "Acme",
"email": "[email protected] /cdn-cgi/l/email-protection",
"matches": [171844, 169729, 173168, 174310, 168752, 174972, 172959, 169546]
}
{
"_id": 2,
"name": "Bruce Wayne",
"company": "Wayne Enterprises",
"email": "[email protected] /cdn-cgi/l/email-protection",
"matches": [171844, 232333, 233312, 123456]
}
{
"_id": 3,
"name": "Tony Stark",
"company": "Stark Industries",
"email": "[email protected] /cdn-cgi/l/email-protection",
"matches": [173844, 155729, 133168, 199310, 132752, 139972]
}
{
"_id": 4,
"name": "Clark Kent",
"company": "Daily Planet",
"email": "[email protected] /cdn-cgi/l/email-protection",
"matches": [169729, 174310, 168752]
}
{
"_id": 5,
"name": "Lois Lane",
"company": "Daily Planet",
"email": "lois.lan[email protected] /cdn-cgi/l/email-protection",
"matches": [172959, 169546]
}
我需要获取经过筛选的用户列表,但有一个键可以根据用户拥有的“匹配”记录数显示用户的“排名”位置。
应该有一个global ranking
位置和一个company ranking
位置。
期望的结果应该是这样的(例如过滤 company='Daily Planet'):
{
_id: 4,
name: 'Clark Kent',
company: 'Daily Planet',
email: '[email protected] /cdn-cgi/l/email-protection',
points: 3, // <=
globalRank: 4, // <=
companyRank: 1 // <=
},
{
_id: 5,
name: 'Lois Lane',
company: 'Daily Planet',
email: '[email protected] /cdn-cgi/l/email-protection',
points: 2, // <=
globalRank: 4, // <=
companyRank: 2 // <=
}
请注意,Clark Kent 在全球排名中排名第 4,因为他有 3 场比赛(John Doe、Bruce Wayne 和 Tony Stark 的比赛比他多),并且在公司排名中排名第一,因为他比任何 Daily Planet 用户的比赛都多。 。
然而,经过几天的研究,我还是找不到办法。 (我什至不知道如何仅进行全球排名或公司排名)。
关于如何解决这个问题,或者如何以不同的方式解决这个问题有什么想法吗?