Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
how to calculate only two user sum in mongoDB not all user sum

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
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!