我在mongo中有一个像这样的值
{ id: Mongo ID,
Match_Type: Challenge Match,
Match_Number: 0001
Challenging_Player: Mark,
Challenged_Player: John,
}
{ id: Mongo ID,
Match_Type: Challenge Match,
Match_Number: 0002
Challenging_Player: John,
Challenged_Player: Bob
}
名称可以出现在Challenging Player
或Challenged Player
的两列中。我正在尝试进行汇总,以找出参加挑战赛次数最多的玩家。这意味着我需要一个既有挑战性又有挑战性的球员分组。
有人可以帮忙吗?
答案 0 :(得分:2)
db.getCollection('the_collection').aggregate([
{$match: { Match_Type: "Challenge Match"}},
{ $project: { players: { $setUnion: [ ["$Challenging_Player"], ["$Challenged_Player"] ] } }},
{ "$unwind" : "$players"},
{$group : { _id:"$players", count:{$sum:1} } }
])
答案 1 :(得分:1)
db.<yourcollection>.aggregate( [
{ $project : { players: ["$Challenging_Player", "$Challenged_Player"]}},
{ "$unwind" : "$players"},
{ "$group" : {"_id" : "$players", "count" : {"$sum" : 1}}}
] )
我要将您的每个文档都分解为2个文档,每个玩家一个,然后按该字段分组