2

I want to sum total of amount with the type string in array so I write this in mongo. any help please

Tried :

db.compte.aggregate([
 {$project:{
   account: "$auditMessages.eventMetaData.Counterparty Name",
   amount: "$auditMessages.eventMetaData.Transaction Amount",
   "total": {$sum:"$amount"}
 }}])

The result:

{
"_id" : ObjectId("5ed638e566a699750cbb1b0f"),
"account" : [
  "Mr Jalel CHAHED",
  "Mr Jalel CHAHED",
  "Mr Jalel CHAHED",
  "Mr Jalel CHAHED"
 ],
"amount" :
 [ 
  "4.32",
  "4.32",
   "1.32",
   "1.32"
 ],
"total" : 0}
4
  • What is the issue with current query? What is the current result? What is the expected result? Commented Jun 10, 2020 at 13:28
  • I want to sum total of amount, sum is 11.28 (4.32+4.32+1.32+1.32) but the total is 0 Commented Jun 10, 2020 at 14:08
  • This should be easy just group by id or main column here and then do sum on amount. You will need another pipeline maybe. As we don't know what the compte collection look like we can't provide any specific solution to this question. Commented Jun 10, 2020 at 14:11
  • I used group by, the same result total 0 Commented Jun 10, 2020 at 14:17

1 Answer 1

2

You're creating amount field in $project and using it in same stage there itself which is why it's returning 0 on "total": {$sum:"$amount"}, also $sum seems to not take arrays.

Assuming your documents look like :

{
    "auditMessages": {
      eventMetaData: [
        {
          "Counterparty Name": "Mr Jalel CHAHED",
          abc: 1,
          "Transaction Amount": "4.32"
        },
        {
          "Counterparty Name": "Mr Jalel CHAHED",
          abc: 11,
          "Transaction Amount": "4.32"
        },
        {
          "Counterparty Name": "Mr Jalel CHAHED",
          abc: 12,
          "Transaction Amount": "1.32"
        },
        {
          "Counterparty Name": "Mr Jalel CHAHED",
          abc: 122,
          "Transaction Amount": "1.32"
        }
      ]
    }
  }

As auditMessages.eventMetaData is an array then when you do :

"$auditMessages.eventMetaData.Counterparty Name" or "$auditMessages.eventMetaData.Transaction Amount" then what you get is an array of values like what you see in your result as of now.

Query :

db.collection.aggregate([
    {
      $project: {
        account: { $arrayElemAt: [ "$auditMessages.eventMetaData.Counterparty Name", 0 ] }, // get name from first element in array,use only if name is same in all objects
        total: {
          $reduce: {
            input: "$auditMessages.eventMetaData.Transaction Amount", // Iterate on array
            initialValue: 0,
            in: { $trunc: [ { $add: [ "$$value", { $toDouble: "$$this" } ] }, 2 ] } // convert string to double, sum-up & truncate to 2 digits
          }
        }
      }
    }
  ])

Test : mongoplayground

Ref : aggregation

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

2 Comments

how can get one name for example name Mr Jalel CHAHED and the total number is 4 thank you!
@m1h3r: Didn't get what you mean by !

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.