Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.97k stars 3.84k forks source link

Aggregate cursor exec is not a function #10862

Closed adamreisnz closed 3 years ago

adamreisnz commented 3 years ago

Do you want to request a feature or report a bug? Bug, or outdated documentation

What is the current behavior? Referring to: https://mongoosejs.com/docs/api/aggregate.html#aggregate_Aggregate-cursor The example states:

const cursor = Model.aggregate(..).cursor({ batchSize: 1000 }).exec();
cursor.eachAsync(function(doc, i) {
  // use doc
});

However, our code yields:

TypeError: Model.aggregate(...).collation(...).cursor(...).exec is not a function

Removing the exec part returns a cursor as expected.

If the current behavior is a bug, please provide the steps to reproduce. See above

What is the expected behavior? For exec to work, or docs to be updated to no longer require this.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version. Mongoose 6.0.7

vkarpov15 commented 3 years ago

The docs are outdated, we removed the need to use exec() for aggregation cursors in 6.0 with #10410. Will update the docs :+1:

adamreisnz commented 3 years ago

Sweet as, is it still required/advisable to use .exec() after regular querying, e.g. find, findOne, updateOne, etc?

vkarpov15 commented 3 years ago

@adamreisnz using exec() after querying is not required (unless you're using cls-hooked or similar libs), but advisable for cleaner stack traces.

ligolas commented 2 years ago

It seems the changes between mongoose 5 and mongoose 6 caused your problem. In mongoose 5 aggregate.cursor return this(the aggregate object), not the promise, and you have toexec() to get the promise. In mongoose 6 aggregate.cursor() return AggregateCursor just like QueryCursor ( query.cursor() return QueryCursor in mongoose 5 ) @adamreisnz