Wednesday, 7 June 2017

Mongodb aggregation query with condition counter increase

var data_form = {
  {
    _id : "123",
    result:{
      run:10
    },
    result_re:{
      run:10
    },
    result_ch:{
      run:10
    },
    result_qm:{
      run:10
    }
  },
  {
    _id : "345",
    result:{
      run:20
    },
    result_re:{
      run:20
    },
    result_ch:{
      run:20
    },
    result_qm:{
      run:20
    }
  },
  {
    _id : "567",
    result:{
      run:30
    },
    result_re:{
      run:30
    },
    result_ch:{
      run:30
    },
    result_qm:{
      run:30
    }
  }
}

var pipeline = [
      { $project: { 

          total: { $add: [ "$result.run", "$result_re.run", "$result_ch.run", "$result_qm.run"] } ,               
          discount:{  
                      $cond: [ { $gt: [ total , 50 ] }, 1, 0]
                   }
          }
      },
      { $sort: {total: -1}},
      { $limit : 10 }
]

db.getCollection('game_users').aggregate(pipeline)

I need to compare total output with aggregation condition and counter increase if condition match.

My collection is defined in data_form variable.

total field output get from query and if that total is grater than 50 after that counter increase.



via Jay Jariwala

No comments:

Post a Comment