Thursday, 25 May 2017

Mongoose find min/max count from collection

I have this function to retrieve the count of some field:

modelFunctions.conteoBatch = (options, callback) => {

  workOrderModel.aggregate(
    {
    $unwind: "$batches"
  },
  {
    $group: {
      _id: "$batches",
      total: {
        $sum: 1
      }
    }
  }, function(err, conteos) {

    if (err) {
      callback(err, conteos);
    }
    //console.log(conteos);

    callback(err, conteos);
  });
}

and I get this as result:

[ { _id: 591c9a702d67ae17a4408c48, total: 2 },
  { _id: 591c9a702d67ae17a4407d5r, total: 5 },
  { _id: 591c9a9e5683f81b8c48e44c, total: 8 } ]

How can I get only the min and max count using the same function. Some like this

[ { _id: 591c9a702d67ae17a4408c48, min: 2 },
  { _id: 591c9a9e5683f81b8c48e44c, max: 8 } ]

Or I have to create two functions, one for the max and one for the min?

Thanks in advance



via joselegit

No comments:

Post a Comment