0

I am doing a little hobby project for friends' disc golf results.

I'm fairly new to MongoDB and I've stuck on a query. What I want to do is to return a JSON object on the form [{player: "Name", totalThrows: (sum of Totalt field), total+/-: (sum of +/- field)}].

So in short: I want to aggregate each players total throws (Total field) and total+/-, over 3 different documents in my database.

The final JSON would look like this: [{player: "Tormod", totalThrows: 148, total+/-: 24}, {player: "Martin", totalThrows: 149, total+/-: 25}, {player: "Andreas", totalThrows: 158, total+/-: 34}]

The picture below shows my document as it's saved in MongoDB. The results array consist of the results of a specific player discgolf results. There is also not a set order on which player is first in the results array in the different documents. Each document represents a new round (think of playing 3 different days).

enter image description here

Code:

aggregate(
        [
            {$unwind: "$results"},
            {$group: { 
                player: "$results.PlayerName",
                throws: "$results.Totalt"
            }},
            {$group: {
                _id: "$_id", 
                totalThrows: {$sum: "$results.Totalt"}
            }}
        ])

https://mongoplayground.net/p/aaKv3aaCeCi

4
  • Can you share what you've tried? Commented Apr 8, 2021 at 0:19
  • So i've been trying alot of different stuff, most recently i've been onto something like this: [ {$unwind: "$results"}, {$group: { player: "$results.PlayerName", throws: "$results.Totalt" }}, {$group: { _id: "$_id", totalThrows: {$sum: "$results.Totalt"} }} ]) Commented Apr 8, 2021 at 0:24
  • Can you add your code to the question and share a MongoPlayground link with some sample data? Commented Apr 8, 2021 at 0:26
  • Done @ArunKumarMohan Commented Apr 8, 2021 at 0:30

1 Answer 1

1

You should pass an _id field (the group by expression) to the $group stage and use $sum to sum the Totalt and +/- values. Since the values are strings, you should convert them to integers using $toInt before summing them. Finally, you can project the values based on the desired structure.

Btw, it looks like most of the numeric fields are stored as strings in the documents. I would recommend you update the documents converting them all to numbers and make sure only numbers are added to the numeric fields in the new documents.

db.collection.aggregate([
  {
    $unwind: "$results",
  },
  {
    $group: {
      _id: "$results.PlayerName",
      totalThrows: {
        $sum: {
          $toInt: "$results.Totalt",
        },
      },
      "total+/-": {
        $sum: {
          $toInt: "$results.+/-",
        },
      },
    },
  },
  {
    $project: {
      _id: 0,
      player: "$_id",
      totalThrows: "$totalThrows",
      "total+/-": "$total+/-",
    },
  },
])

MongoPlayground

Sign up to request clarification or add additional context in comments.

1 Comment

The god! I guess I was semi-close, thank you so much Arun!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.