Mongoose / MongoDB汇总了子文档的数组字段

时间:2017-12-25 09:52:06

标签: node.js mongodb express mongoose

我是MongoDB / Mongoose的新手,我不知道我应该如何得到我想要的结果。基本上我有这样的文件:

{
    "playerId" : "",
    "sessions" : [ 
        {
            "start" : "",
            "end" : "",
            "join" : "",
            "leave" : "",
            "rounds" : [,
                {
                    "name" : "roundName",
                    "weapons" : [
                        {
                            "name" : "weaponName",
                            "kills" : 1,
                            "assits" : 1,
                            "deaths" : 1,
                            "shots" : 1,
                            "headshots" : 1
                        },
                        {
                            "name" : "weaponName",
                            "kills" : 1,
                            "assits" : 1,
                            "deaths" : 1,
                            "shots" : 1,
                            "headshots" : 1
                        }
                    ]
                },
                {
                    "name" : "roundName",
                    "weapons" : [
                        {
                            "name" : "weaponName",
                            "kills" : 1,
                            "assits" : 1,
                            "deaths" : 1,
                            "shots" : 1,
                            "headshots" : 1
                        },
                        {
                            "name" : "weaponName",
                            "kills" : 1,
                            "assits" : 1,
                            "deaths" : 1,
                            "shots" : 1,
                            "headshots" : 1
                        }
                    ]
                },
                {
                    "name" : "roundName",
                    "weapons" : [
                        {
                            "name" : "weaponName",
                            "kills" : 1,
                            "assits" : 1,
                            "deaths" : 1,
                            "shots" : 1,
                            "headshots" : 1
                        },
                        {
                            "name" : "weaponName",
                            "kills" : 1,
                            "assits" : 1,
                            "deaths" : 1,
                            "shots" : 1,
                            "headshots" : 1
                        }
                    ]
                }
            ]
        }
    ]
}

我想要的是一个查询,为每一轮(杀戮,死亡,助攻,爆头,投篮)添加字段,其中包含所有武器的总和。如果可能的话,整个会话也应该如此。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

以下内容应该为您提供按会话 - >轮次分组的统计信息列表: 这里的假设是1)集合的名称是射击游戏2)字段的名称是$ sessions.Rounds.Weapons

db.shootingGame.aggregate(
   [
       {$group: {
       _id: "$sessions.Rounds", 
       "sumkills": {$sum: "$sessions.Rounds.Weapons.kills" },
       "sumasists": {$sum: "$sessions.Rounds.Weapons.assists" },
       "sumdeaths": {$sum: "$sessions.Rounds.Weapons.deaths" },
       "sumheadshots": {$sum: "$sessions.Rounds.Weapons.headshots" },
       "sumshots": {$sum: "$sessions.Rounds.Weapons.shots" }
   }}
   ]
)

但是对于每个会话的进一步分组,您必须将此结果存储到另一个文档中并且每轮运行另一个$ sum组