Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!