我如何使用猫鼬聚合函数基于每次匹配计算(相对于计数,获胜率)计数?我有以下收藏:
用户集合:
{
"_id": "5d1318f8bf93dd6d9aee05dd",
"name": "A",
"tribe": "human",
"point": 1100
},
{
"_id": "5d197979f984b60edf070cfd",
"name": "B",
"tribe": "elf",
"point": 1000
},
... more
比赛集合:
{
"_id": "5d242b3244f1734f02d62883",
"mathcedAt": "2019-07-09",
"winner": { /* user model ({type: 'ObjectId', ref: 'User'}) */
"_id": "5d1318f8bf93dd6d9aee05dd",
"name": "A",
"tribe": "human",
"point": 1100
},
"loser": { /* user model ({type: 'ObjectId', ref: 'User'}) */
"_id": "5d197979f984b60edf070cfd",
"name": "B",
"tribe": "elf",
"point": 1000
},
},
... more
我想要的结果
1)应基于所有用户和每个点显示排名。
2)与对手的比赛结果也应包括每个部落的结果。 (例如vsElf,vsHuman ..)
{
"user": {
"_id": "5d1318f8bf93dd6d9aee05dd",
"name": "A",
"tribe": "human",
"point": 1100
},
"ranking": 1, // Based on all users and each point
"totalCount": {
"win": 1,
"lose": 0,
}
"winCount": {
"vsElf": 1,
"vsHuman": 0,
},
"loseCount": {
"vsElf": 0,
"vsHuman": 0,
},
"winRate": {
"vsElf": "100%",
"vsHuman": "",
}
},
{
"user": {
"_id": "5d197979f984b60edf070cfd",
"name": "B",
"tribe": "elf",
"point": 1000
},
"ranking": 2, // Based on all users and each point
"totalCount": {
"win": 0,
"lose": 1,
}
"winCount": {
"vsElf": 0,
"vsHuman": 0,
},
"loseCount": {
"vsElf": 0,
"vsHuman": 1,
},
"winRate": {
"vsElf": "100%",
"vsHuman": "",
}
}
... more
如何使用猫鼬获得以上结果? 我尝试了很多方法,但是没用。.
非常感谢