how to calculate only two player sum . for example i have player schema
and in mongoDB i have 10 players data but i want to calculate sum of two player data like this =>player1(win=1)+player2(win=1) = total_win = 2
const player_Schema = new mongoose.Schema({
joining_date:{
type:String,
required:true
},
name:{
type:String,
required:true
},
tip:{
type:Number,
required:true
},
win:{
type:Number,
required:true
},
balance:{
type:Number,
required:true
},
tournament_id:{
type: mongoose.Schema.Types.ObjectId,
ref:"tournament_datas"
}
})
i use the aggregation with group but in result got all ten players(winner) sum
working code
const total_tip = db.aggregate([
// {$limit:2},
{$match:{ _id:tournamentId} },
{$group : {
_id:null,
total_tip:{$sum:"$tip"},
total_win:{$sum:"$win"}
}}
], (err,data) => {
if(err) throw err;
console.log("total_tip ==>",data);
})
i got two player winner sum using limit but it's not good .
what i need when
a) while creating the players and tournaments , calculate the totals(totalWin,totalTip) from players by aggregation.<br>
e.g: player1,tournament1,tip=25,win=75
player2,tounament1,tip=10,win=40
Then the tournament 1 will have totalTip:25+10=35 and totalWin:75+40=115
e.g: player1,tournament2,tip=20,win=100
player3,tournament2,tip=30,win=70
Then the tournament 2 will have totalTip:20+30=50 and totalWin:100+70=170
and so on for the tournament and players