obtuve este error: los argumentos deben ser operadores de canalización agregados, este es el curso 'Esquema' que obtuvo bootcamp como objectId.
const CourseSchema = new mongoose.Schema({ bootcamp: { type: mongoose.Schema.ObjectId, ref: 'Bootcamp', required: true } });la agregación:
//static method to get avg of course tuitions CourseSchema.statics.getAverageCost = async function (bootcampId) { console.log('calculating avg cost... with bootcampId:' + bootcampId); const obj = await this.aggragate([{ $match: { bootcamp: bootcampId }, $group: { _id: '$bootcamp', averageCost: { $avg: '$tuition' } } }]); console.log(obj); }llamando a la agregación antes de guardar o eliminar:
...
// Call getAvarageCost after save CourseSchema.post('save', function () { this.constructor.getAverageCost(this.bootcamp); }) // Call getAvarageCost before remove CourseSchema.post('remove', function () { this.constructor.getAverageCost(this.bootcamp); })...
$coincidencia y $grupo deben estar en diferentes operaciones de canalización
const cursor = this.aggregate([ { $match: { bootcamp: bootcampId } }, { $group: { _id: '$bootcamp', averageCost: { $avg: '$tuition' }, }, }, ]) console.log(await cursor.toArray())