MongoDB running total like aggregation of previous

2019-08-04 01:07发布

I am currently dealing with a set of in-game-events for various matches. In the game it is possible to kill enemies and purchase items in a shop.

What I have been trying to do right now, is to count the number of kills that have occured in a single match up until every purchasing event.

{
    "_id" : ObjectId("5988f89ae5873exxxxxxx"),
    "gameId" : NumberLong(2910126xxx)
    "participantId" : 3,
    "type" : "ITEM_PURCHASED",
    "timestamp" : 656664 },    
{
    "_id" : ObjectId("5988f89ae5873exxxxxxx"),
    "gameId" : NumberLong(2910126xxx)
    "participantId" : 3,
    "victimId" : 9,
    "type" : "ENEMY_KILLED",
    "timestamp" : 745245 },
{
    "_id" : ObjectId("5988f89ae5873exxxxxxx"),
    "gameId" : NumberLong(2910126xxx)
    "participantId" : 3,
    "victimId" : 7,
    "type" : "ENEMY_KILLED",
    "timestamp" : 746223 },
{
    "_id" : ObjectId("5988f89ae5873exxxxxxx"),
    "gameId" : NumberLong(2910126xxx)
    "participantId" : 3,
    "type" : "ITEM_PURCHASED",
    "timestamp" : 840245 },    

In which this is a desired outcome for me:

{
    "_id" : ObjectId("5988f89ae5873exxxxxxx"),
    "gameId" : NumberLong(2910126xxx)
    "participantId" : 3,
    "type" : "ITEM_PURCHASED",
    "timestamp" : 656664,
    "kills": 0  },
 {
    "_id" : ObjectId("5988f89ae5873exxxxxxx"),
    "gameId" : NumberLong(2910126xxx)
    "participantId" : 3,
    "type" : "ITEM_PURCHASED",
    "timestamp" : 840245 ,
     "kills": 2  }

Although I am inclined to think that this is quite impossible, I am not yet that experienced with all the capabilities mongo offers.

Is there a way to count occurrances of certain values up until the appearance of an purchasing event?

1条回答
Root(大扎)
2楼-- · 2019-08-04 01:58

try this aggregation

  1. $match - filter by gameId
  2. $sort - order documents by timestamp
  3. $group - accumulate all matched to an array
  4. $addFields - $reduce to calculate kills, filter and map kills to document
  5. $unwind - flat array to get original document structure
  6. $replaceRoot - move data to top level as in original structure

pipeline

db.games.aggregate([
    {$match : {gameId : 1}},
    {$sort : {timestamp : 1}},
    {$group : {_id : "$gameId", data : {$push : "$$ROOT"}}},
    {$addFields : {data : {
        $reduce : {
            input : "$data",
            initialValue : {kills : [], data : [], count : 0},
            in : {
                count : {$sum : ["$$value.count", {$cond : [{$eq : ["$$this.type", "ENEMY_KILLED"]}, 1, 0]}]},
                data : { $concatArrays : [
                     "$$value.data", 
                     {$cond : [
                            {$ne : ["$$this.type", "ENEMY_KILLED"]}, 
                            [
                                {
                                    _id : "$$this._id",
                                    gameId : "$$this.gameId",
                                    participantId : "$$this.participantId",
                                    type : "$$this.type",
                                    timestamp : "$$this.timestamp",
                                    kills : {$sum : ["$$value.count", {$cond : [{$eq : ["$$this.type", "ENEMY_KILLED"]}, 1, 0]}]}
                                }
                            ],
                            []
                        ]}
                    ]}
                }
            }}
    }},
    {$unwind : "$data.data"},
    {$replaceRoot : {newRoot : "$data.data"}}
]).pretty()

collection

> db.games.find()
{ "_id" : 1, "gameId" : 1, "participantId" : 3, "type" : "ITEM_PURCHASED", "timestamp" : 656664 }
{ "_id" : 2, "gameId" : 1, "participantId" : 3, "victimId" : 9, "type" : "ENEMY_KILLED", "timestamp" : 745245 }
{ "_id" : 3, "gameId" : 1, "participantId" : 3, "victimId" : 7, "type" : "ENEMY_KILLED", "timestamp" : 746223 }
{ "_id" : 4, "gameId" : 1, "participantId" : 3, "type" : "ITEM_PURCHASED", "timestamp" : 840245 }

result

{
    "_id" : 1,
    "gameId" : 1,
    "participantId" : 3,
    "type" : "ITEM_PURCHASED",
    "timestamp" : 656664,
    "kills" : 0
}
{
    "_id" : 4,
    "gameId" : 1,
    "participantId" : 3,
    "type" : "ITEM_PURCHASED",
    "timestamp" : 840245,
    "kills" : 2
}
> 
查看更多
登录 后发表回答