# Aggregation based on key in array of objects

**URL:** <https://community.parseplatform.org/t/aggregation-based-on-key-in-array-of-objects/1019>\
**Category:** REST API\
**Created:** [November 12, 2020, 10:49am UTC](https://community.parseplatform.org/t/aggregation-based-on-key-in-array-of-objects/1019 "2020-11-12T10:49:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eugene](https://community.parseplatform.org/user_avatar/community.parseplatform.org/eugene/32/341_2.png) [@eugene](https://community.parseplatform.org/u/eugene)\
**Post date:** [November 12, 2020, 10:49am UTC](https://community.parseplatform.org/t/aggregation-based-on-key-in-array-of-objects/1019/1 "2020-11-12T10:49:52Z")

</div>

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

```auto
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:

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

```

The response looks something like this:

```auto
{
    "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?

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

```

Thanks very much for the help in advance!

---

<div class="post-metadata">

**Author:** ![davimacedo](https://community.parseplatform.org/user_avatar/community.parseplatform.org/davimacedo/32/316_2.png) [@davimacedo](https://community.parseplatform.org/u/davimacedo)\
**Post date:** [November 12, 2020, 9:44pm UTC](https://community.parseplatform.org/t/aggregation-based-on-key-in-array-of-objects/1019/2 "2020-11-12T21:44:17Z")

</div>

Try with:

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

```

---

<div class="post-metadata">

**Author:** ![eugene](https://community.parseplatform.org/user_avatar/community.parseplatform.org/eugene/32/341_2.png) [@eugene](https://community.parseplatform.org/u/eugene)\
**Post date:** [November 13, 2020, 10:08am UTC](https://community.parseplatform.org/t/aggregation-based-on-key-in-array-of-objects/1019/3 "2020-11-13T10:08:19Z")

</div>

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

```auto
{
    "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:

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

```

Again, thanks for your help in advance!

---

<div class="post-metadata">

**Author:** ![davimacedo](https://community.parseplatform.org/user_avatar/community.parseplatform.org/davimacedo/32/316_2.png) [@davimacedo](https://community.parseplatform.org/u/davimacedo)\
**Post date:** [November 13, 2020, 5:38pm UTC](https://community.parseplatform.org/t/aggregation-based-on-key-in-array-of-objects/1019/4 "2020-11-13T17:38:50Z")

</div>

You need to first unwind players. Try with:

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

```

---

<div class="post-metadata">

**Author:** ![eugene](https://community.parseplatform.org/user_avatar/community.parseplatform.org/eugene/32/341_2.png) [@eugene](https://community.parseplatform.org/u/eugene)\
**Post date:** [November 16, 2020, 4:01am UTC](https://community.parseplatform.org/t/aggregation-based-on-key-in-array-of-objects/1019/5 "2020-11-16T04:01:18Z")

</div>

> [@davimacedo](#):
>
> `unwind=$players&group={"objectId":{"steamId" : "$players.steamId"},"total":{"$sum":"$players.kills"}}`

that’s exactly it! thanks a million!

---

<div class="post-metadata">

**Author:** ![eugene](https://community.parseplatform.org/user_avatar/community.parseplatform.org/eugene/32/341_2.png) [@eugene](https://community.parseplatform.org/u/eugene)\
**Post date:** [November 16, 2020, 4:03am UTC](https://community.parseplatform.org/t/aggregation-based-on-key-in-array-of-objects/1019/6 "2020-11-16T04:03:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![davimacedo](https://community.parseplatform.org/user_avatar/community.parseplatform.org/davimacedo/32/316_2.png) [@davimacedo](https://community.parseplatform.org/u/davimacedo)\
**Post date:** [November 16, 2020, 11:34pm UTC](https://community.parseplatform.org/t/aggregation-based-on-key-in-array-of-objects/1019/7 "2020-11-16T23:34:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eugene](https://community.parseplatform.org/user_avatar/community.parseplatform.org/eugene/32/341_2.png) [@eugene](https://community.parseplatform.org/u/eugene)\
**Post date:** [November 18, 2020, 9:08am UTC](https://community.parseplatform.org/t/aggregation-based-on-key-in-array-of-objects/1019/8 "2020-11-18T09:08:28Z")

</div>

> [@davimacedo](#):
>
> &sort={“$total”:1

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