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

问题描述 投票:0回答:1

我是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
                        }
                    ]
                }
            ]
        }
    ]
}

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

node.js mongodb express mongoose
1个回答
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组

© www.soinside.com 2019 - 2024. All rights reserved.