Skip to content

AggregationCursor will not emit errors like QueryCursor when errors thrown in pre hook. #15279

@jsas

Description

@jsas

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.10.0

Node.js version

20.15

MongoDB server version

5

Typescript version (if applicable)

No response

Description

Using Model.aggregate.cursor() will not emit errors like QueryCursor when errors thrown in pre hook.

schema.pre('aggregate', function(next) {
  if (!this.options.allowed) {  
    throw new Error(`hey, that's nacho cheese!`)
  }
  next()
})

// -----------

// this would throw
const res = await Model.aggregate([{ $limit: 1 }], { allowed: false }).exec()
return res

// this will continue and stream results
const cursor = Model.aggregate([{ $limit: 1 }], { allowed: false }).cursor()
const aahsss = []
for await(const it of cursor) {
   aahsss.push(it)
}

QueryCursor seems to have this implemented. Is this by design, because aggregations aren't "ORMey"?

Steps to Reproduce

  • create a schema with a pre('aggregate') hook that throws an error
  • use Model.aggregate([{$limit: 1 }]).cursor()
  • see a document rather that an error thrown or emitted

Expected Behavior

expect aggregation cursor to emit an error from a failed pre('aggregate') hook

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions