aggregate 实战一


aggregate 实战一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 玩家重名
let playersDataList = await playersDataModel.aggregate([
{$match: {mAreaId: 1}},
{$group: {_id: "$mRoleName", obj: {$push: {mId: "$mId", mAreaId: "$mAreaId"}}, count: {$sum: 1}}},
{$match: {count: {$gt: 1}}}
]);
if(playersDataList && playersDataList.length > 0){
for (let i = 0; i < playersDataList.length; i++) {
// { "_id" : "战士" , "obj" : [ { "mId" : "97d50fa6-3425-44cf-9c4f-a0aa5d952cd0", "mAreaId" : "1" }, { "mId" : "2303b736-1993-4c07-8fc6-be92b8867d73", "mAreaId" : "2" } ], "count" : 2 }
const element = playersDataList[i];
for (let j = 0; j < element.obj.length; j++) {
const ele = element.obj[j];
....
}
}
}