0

Im using Node.js with Mongoose to Store data in MongoDB.

I need to show a list of all the users with their current available balance. I tried using multiple for loops, but I kept getting errors somewhere or the other. I'm able to show the list of unique users, but summation of their balance is incorrect. I felt I was doing it the wrong way. Could someone suggest a good way to get my required output with lesser and pretty code?

Current Data in My Database:

ID    Name    Amount    Credit     
1     Andy     500       true     
2     Andy    1000       false
3     Tina     400       false
4     John     700       true
5     Tina    2000       true 
6     Andy     100       true 
7     Tina     200       false 
8     John     300       false 

Required Final Output

 ID    Name   Balance 
 1     Andy    -400
 2     Tina    1400
 3     John     400

What I have tried till now:

    Accounts.find({}).exec(function (err, transList) {

          var finalArr= [];

          var currentAmount = 0;
          for(var i=0; i<transList.length; i++)
          {

                    if(transList[i].status===true) // Credited
                     {
                           currentAmount = currentAmount + transList[i].amount;
                     }
                     else // Debited
                     {
                           currentAmount = currentAmount - transList[i].amount;
                     }
                if(i===0)
                {

                }
                else if (i===transList.length-1)
                {


                      var obj  = {};
                      obj._id  = transList[i]._id;
                      obj.name  = transList[i].name;
                      obj.amount = currentAmount;
                      finalArr.push(obj);
                }
                else
                {
                   if(transList[i].student._id!=transList[i-1].student._id)
                   {

                      var obj  = {};
                      obj._id  = transList[i-1]._id;
                      obj.name  = transList[i-1].name;
                      obj.amount = currentAmount;
                      finalArr.push(obj);
                      currentAmount = 0;
                   }
                                       }
          }

          data = JSON.stringify({list:finalArr});
          res.end(data);

        });
Anirudh
  • 2,767
  • 5
  • 69
  • 119
  • 1
    I can suggest to research this option: https://docs.mongodb.com/manual/reference/operator/aggregation/group/ – Lazyexpert Jul 28 '17 at 14:48
  • See [SQL to Aggregation Mapping Chart](https://docs.mongodb.com/manual/reference/sql-aggregation-comparison/) if you have any familiarity with SQL, then most common cases are given with an example of how you would do the same with MongoDB. – Neil Lunn Jul 28 '17 at 23:42
  • 1
    In a nutshell though you want [`$cond`](https://docs.mongodb.com/manual/reference/operator/aggregation/cond/) `{ "$group": { "_id": "$name", "balance": { "$sum": { "$cond": ["$credit", "$amount", { "$subtract": [ 0, "$amount" ] }] }} }}` – Neil Lunn Jul 28 '17 at 23:47
  • @NeilLunn solved it using aggregation, but got stuck here. https://stackoverflow.com/questions/45388917/match-not-working-with-aggregation-in-mongoose – Anirudh Jul 29 '17 at 12:03

0 Answers0