Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
trying to calc average with $avg and got: arguments must be aggregate pipeline operators

got this error: arguments must be aggregate pipeline operators this is the course 'Schema" that got bootcamp as objectId.

   const CourseSchema = new mongoose.Schema({

   bootcamp: {
        type: mongoose.Schema.ObjectId,
        ref: 'Bootcamp',
        required: true
    }
  });

the aggregation :

 //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);
    }

calling for the aggregation before saving or removing:

...

// 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);
})

...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

$match and $group must be in different pipeline operations

const cursor = this.aggregate([
  { $match: { bootcamp: bootcampId } },
  {
    $group: {
      _id: '$bootcamp',
      averageCost: { $avg: '$tuition' },
    },
  },
])
console.log(await cursor.toArray())
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda