Node.js - Mongooose + Aggreagte

摘要:Node.js - Mongooose + Aggreagte

var mongoose = require('mongoose'),
    config = require('./config');
 
mongoose.connect(config.db.host, {
    user: config.db.user,
    pass: config.db.pass
});
 
var StatSchema = new mongoose.Schema({
    fieldA: String,
    fieldB: String,
    fieldC: String,
    total: String
});
 
exports.getStat = function (req,callback){
     var  table = "stat";
     var StatModel = mongoose.model(table, StatSchema , table);
    console.log('model after');
     StatModel 
    .aggregate({$group:{_id:{fieldA:"$fieldA",fieldB:"$fieldB",fieldC:"$fieldC"},total:{$sum:1}}}
, {$project: { fieldA:1,fieldB:1,fieldC:1,total:1}}
, function (err, res) {
         if (err) 
               callback(err);
         else
         {
                console.log(res);
                callback(null, res);
         }
      }
);