Q:

how to make aggregrate function dynamic using params in express and mongoose

I am using aggregate function in mongoose to fetch some data, here its static implementation
app.get("/male",function (req,res) {
  Record.aggregate([
    {
      $match: 
      {"gender": "male"}
    },
      {
         $group:{ 
            _id : "$type",
             total : {$sum : 1}
        }
      },{
      $sort: {_id: 1}
    }
      ]).exec((err,data) => {
            if (err) {console.log(err)} 

            res.json(data)

        })

})

I wanna make it completely dynamic so i tried this
  app.get("/:query/:type/:match",function (req,res) {

  var match = req.params.match

  Record.aggregate([
    {
      $match: 
      {match : req.params.type}
    },
      {
         $group:{ 
            _id : "$"+req.params.query,
             total : {$sum : 1}
        }
      },{
      $sort: {_id: 1}
    }
      ]).exec((err,data) => {
        if (err) {console.log(err)}

              res.json(data)

        })

})

I debugged it little bit ,it appears that match is not passing in $match,
if i put static variable instead of match it works
Here its scheema
   var mongoose = require('mongoose');

   var RecordSchema = new mongoose.Schema({

      type:String,
      gender:String,
      age:Number,
      timeSpent:Number,
      arrivedAt:Number

   })

   module.exports = mongoose.model("Record", RecordSchema);

A:

try this way :  
app.get("/:query/:type/:match",function(req,res) {

  var match = req.params.match;
  var type= req.params.type;
  var query = "$"+req.params.query;
  var matchCriteria = {};
  matchCriteria[match]=type;

  Record.aggregate([
    {
      $match:matchCriteria
    },
    {
         $group:{ 
             _id : query,
             total:{$sum:1}
        }
    },{
      $sort: {_id: 1}
     }
   ]).exec((err,data) => {
        if (err) {console.log(err)}
              res.json(data)
        });
});

