简体   繁体   中英

Mongoose/MongoDB sum up fields of an array for subdocuments

I am rather new to MongoDB/Mongoose and I have no idea how I am supposed to get the result I am looking for. Basically I have a document like this:

{
    "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
                        }
                    ]
                }
            ]
        }
    ]
}

What I want is a query that adds fields to each round (kills, deaths, assists, headshots, shots) which contain the sum of all their weapons. The same should happen for the entire session if possible. Any help would be greatly appreciated!

the following should give you list of stats grouping by session->rounds: the assumption here is 1) name of the collection is shootingGame 2) name of the fields are $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" }
   }}
   ]
)

But for further grouping per session, you have to store this result into another document and run another $sum group per round

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM