1. 程式人生 > >ndoejs+mongodb 開發1:aggregate 分組求和

ndoejs+mongodb 開發1:aggregate 分組求和

/**auother : zhz
 * mongodb :分組求和
 * array:
 *      控制的顯示列$project,篩選條件 $match ,排序 $sort,分組key $group:_id ,求和值  $sum($max,$avg,$min等)
 * */
HelperMongo.prototype.mGgruopAndsum = function(collectionName, array,callback) {
    var self = this;
    var reqId = _reqId++;
    _log(reqId,"開始查詢數量... "+collectionName+","+JSON.stringify(array));
    function process(callback) {
        _getCollection(self, collectionName, function (collection) {
            collection.aggregate(array,function(error,rst){
                if (error) {
                    global.err(collectionName + " : " + JSON.stringify(error));
                    return;
                }
                _log(reqId, "查詢成功--> " + JSON.stringify(rst));
                if(callback != undefined) callback(rst);
            });

        });
    }
    return _judgePromise(process,callback);
};
function _judgePromise(process,callback){
    if(callback==undefined){
        return new global.Promise(function(resolve, reject) {
            process(resolve);
        });
    }else{
        process(callback);
    }
}

//獲得排名資訊
    function _get_rank_list(callback){
      constructcommon_exports._get_curr_constructid(function(res) {
          constructid  = res.constructid;

              app.mgCross.mGgruopAndsum(playerRecord.NAME,[
                  { $project : {              //供使用的列
                      constructid   : 1,
                      self_gpid     : 1,
                      is_victory    : 1,
                      stars         : 1,
                      remain_time   : 1,
                      end_time      : 1,
                      curr_ispasslevel   : 1,
                      curr_match : 1
                    }
                  },
                  { $match : {                //篩選條件
                      constructid    : constructid
                     // is_victory     : {"$ne":-1} //沒結算的戰鬥不計入排行
                    }
                  },
                  { $group : {
                      _id               : { self_gpid:"$self_gpid",curr_match:"$curr_match"}, //分組"$self_gpid"
                      curr_levelcounts  : { $sum : "$curr_ispasslevel" }, //運算
                      is_victory        : { $sum : "$is_victory" },
                      stars             : { $sum : "$stars" },
                      remain_time       : { $sum : "$remain_time" },
                      end_time          : { $max : "$end_time"}
                    }
                  }
              ],function(recordlist){

                  if(recordlist.length == 0){
                      var msg = {};
                      msg.rankinfo ={};
                      console.error("沒有排行榜資料");
                      send(msg);
                  }

                  var lable = exUtil.timeUnix();
                  var array = [];
                  for(var i = 0;i<recordlist.length;i++){
                      var doc = {};
                      var tar_gpid_temp = recordlist[i]._id["self_gpid"];
                      var tar_gpsid = tar_gpid_temp.split("-");
                      var tar_gsid  = tar_gpsid[0];
                      var tar_pid   = tar_gpsid[1];
                      doc.constructid = constructid;
                      doc.gsid      = tar_gsid;
                      doc.pid       = tar_pid;
                      doc.curr_rank = lable;
                      doc.level     = (recordlist[i].curr_levelcounts) / 3 ;
                      doc.victory   = recordlist[i].is_victory;
                      doc.star      = recordlist[i].stars;
                      doc.fighttime = recordlist[i].remain_time;
                      doc.overtime  = recordlist[i].end_time;
                      doc.rankingAwards = "";
                      doc.isGetRankingAwards = 0;
                      array.push(doc);
                  }
                  app.mgCross.mgInsert(trank.NAME,array,function(res){ //批量插入
                      callback(constructid,lable);
                  });
              });
          });
    }