我有这个函数来检索某些字段的计数:
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);
});
}
我得到了这个结果:
[ { _id: 591c9a702d67ae17a4408c48, total: 2 },
{ _id: 591c9a702d67ae17a4407d5r, total: 5 },
{ _id: 591c9a9e5683f81b8c48e44c, total: 8 } ]
如何使用相同的功能仅获取最小和最大计数。有些人喜欢这个
[ { _id: 591c9a702d67ae17a4408c48, min: 2 },
{ _id: 591c9a9e5683f81b8c48e44c, max: 8 } ]
或者我必须创建两个函数,一个用于最大值,一个用于最小值?
提前致谢
编辑: 工作单模式:
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);
我需要分组并计算批次