MongoDB running total like aggregation of previous records up until occurrance of value

ぃ、小莉子 提交于 2019-12-24 10:00:36

问题


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:


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
}
> 


来源:https://stackoverflow.com/questions/48956526/mongodb-running-total-like-aggregation-of-previous-records-up-until-occurrance-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!