0

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

EDIT: Work Order Schema:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const utilitiesFunctions = require('./../../../utilities');


const workOrderSchema = Schema({
    code: {
        type: String,
        //required: [true,'Code no puede estar vacio']
    },
    description: {
        type: String
    },
    workOrderTypeId: {
        type: Schema.Types.ObjectId,
        ref: 'work_order_types',
      //  required: [true,'tipo de orden de trabajo no puede estar vacio']
    },
    serieId: {
        type: Schema.Types.ObjectId,
        ref: 'series',
        //required: [true,'Serie no puede estar vacio']
    },
    productId: {
        type: Schema.Types.ObjectId,
        ref: 'products',
        //required: [true,'Producto no puede estar vacio']
    },
    employeeId: {
        type: Schema.Types.ObjectId,
        ref: 'employees',
      //  required: [true,'Empleado no puede estar vacio']
    },
    clientId: {
        type: Schema.Types.ObjectId,
        ref: 'clients',
        //required: [true,'Cliente no puede estar vacio']
    },
    requestedOn: {
        type: Date,
        //required: [true,'Solcitado por no puede estar vacio']
    },
    completedOn: {
        type: Date
    },
    isUrgent: {
        type: Boolean,
        default: false
    },
    batches: {
      type: Schema.Types.ObjectId,
      ref: 'batches',
        //required: [true,'batches no puede estar vacio']
    }
}, {timestamps: true});


const workOrder = module.exports = mongoose.model('work_orders', workOrderSchema);

I need to group and count through batches

joselegit
  • 533
  • 1
  • 14
  • 35

0 Answers0