0

I have two collections i.e. col1 and col2 .
col1 has fields like:
_id
user
payout

Col2 reads like
_id
user
amount


I want output to be sum of payout (from col1) and amount (from col2)

Joe Mutti
  • 137
  • 1
  • 8

1 Answers1

0

why not just find the data in both collections and then pass both objects into a function thats adds to the sum? something like this:

var sum = 0; //do this for both collections db.collection.find({}).forEach(function(obj){ sum+=obj.payout; });

if what you wanted to do was get the overall sum forEach different user you could do something like this:

var sumForUser = {}; //do this for both collections db.collection.find({}).forEach(function(obj){ if ( sumForUser[obj.user] ) { sumForUser[obj.user] += obj.payout; else sumForUser[obj.user] = obj.payout; });

then you would a "sumForUser" object with the sum you wanted for every user. this you would be a "map" of user to sum.

this could also be done in many different ways, including use of reduce functions and other mongoDB functions such as MapReduce and so on. maybe this can be of help. also this and this.

K.Daniel
  • 61
  • 5