Aggregation based on key in array of objects

Hello!I have a field that stores an array of objects that looks like this:

players:
[
  {
    "assists": 0,
    "deaths": 0,
    "hero": "hero_name",
    "team": 2,
    "playerId": 0,
    "steamId": "1234",
    "kills": 0,
  },
  {
    "assists": 0,
    "deaths": 0,
    "hero": "hero_name",
    "team": 1,
    "playerId": 0,
    "steamId": "5678",
    "kills": 0,
  }
]

I’d like to create an aggregate query that returns the total kills based on a user’s steamId, and I’ve tried this query:

group={"objectId":{"steamId" : "$players.steamId"},"total":{"$sum":"$players.kills"}}

The response looks something like this:

{
    "results": [
        {
            "total": 0,
            "objectId": {
                "steamId": [
                    "1234"
                ]
            }
        },
        {
            "total": 0,
            "objectId": {
                "steamId": [
                    "1234",
                    "5678"
                ]
            }
        },
        {
            "total": 0,
            "objectId": {
                "steamId": [
                    "1234",
                    "9876"
                ]
            }
        }
    ]
}

How should I modify the query such that my response returns something like this instead?

{
    "results": [
        {
            "total": 0,
            "objectId": {
                "steamId": [
                    "1234"
                ]
            }
        },
        {
            "total": 0,
            "objectId": {
                "steamId": [
                    "5678"
                ]
            }
        },
        {
            "total": 0,
            "objectId": {
                "steamId": [
                    "9876"
                ]
            }
        }
    ]
}

Thanks very much for the help in advance!

Try with:

group={"objectId":"$players.steamId","total":{"$sum":"$players.kills"}}
1 Like

thanks very much for your response! i tried the suggested query, and i’m still getting the following response:

{
    "results": [
        {
            "total": 0,
            "objectId": [
                "1399",
                "4839"
            ]
        },
        {
            "total": 0,
            "objectId": [
                "4839",
                "1399"
            ]
        },
        {
            "total": 0,
            "objectId": [
                "1399"
            ]
        }
    ]
}

What i’m looking for is for the each result to be per unique steamId so that the response is:

{
    "results": [
        {
            "total": 0,
            "objectId": [
                "1399",
            ]
        },
        {
            "total": 0,
            "objectId": [
                "4839",
            ]
        }
    ]
}

Again, thanks for your help in advance!

You need to first unwind players. Try with:

unwind=$players&group={"objectId":{"steamId" : "$players.steamId"},"total":{"$sum":"$players.kills"}}
1 Like

that’s exactly it! thanks a million!

a follow-up question if you don’t mind, how should i apply a sort/order to total?

I tried doing &sort=$players.kills / &order=$players.kills but got an Invalid parameter for query or Internal server error

Thank you!

I believe it should be &sort={"$total":1}

1 Like

it works with &sort={"total":1 without the $ sign, thanks!!

1 Like