Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AggregationCursor from collection is missing method eachASync #13334

Closed
2 tasks done
andrewlorenz opened this issue Apr 27, 2023 · 4 comments · Fixed by #13448
Closed
2 tasks done

AggregationCursor from collection is missing method eachASync #13334

andrewlorenz opened this issue Apr 27, 2023 · 4 comments · Fixed by #13448
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@andrewlorenz
Copy link

andrewlorenz commented Apr 27, 2023

Prerequisites

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

Mongoose version

7.0.5

Node.js version

16.14.2

MongoDB server version

5.0.12

Typescript version (if applicable)

No response

Description

The observed behaviour of an AggregationCursor from a collection does not align with the documentation at https://mongoosejs.com/docs/api/aggregationcursor.html

Steps to Reproduce

const myDb = await mongoose.createConnection(...)
const myModel = myDb.model('my_collection', Schema)
const myCollection = myDb.collection('my_collection');

const modelA = await myModel.aggregate([ { $match: { name: /test/i } ]);
// modelA contains an array of 1 document as expected

const collFind = await myCollection.find({ name: 'Test' });
// collFind contains 1 document as expected

const collA = await myCollection.aggregate([ { $match: { name: /test/i } ]);
// collA contains an AggregationCursor object

collA.eachAsync(doc => console.log(doc)); - fails is not a function

However, the next() and asyncIterator DO work:

const doc = await collA.next();   // doc contains 1 document
for await (const doc of collA) {
  console.log('doc', doc);    // each doc of collection is output
}

Expected Behavior

The AggregationCursor object is expected to be in line with the documentation, but the eachASync method is missing.
Per the documentation, addCursorFlag and close methods are also available to call.

@andrewlorenz andrewlorenz changed the title AggregationCursor from collection has no methods AggregationCursor from collection has missing methods Apr 27, 2023
@IslandRhythms IslandRhythms added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Apr 28, 2023
@vkarpov15 vkarpov15 added this to the 7.1.2 milestone Apr 28, 2023
@andrewlorenz andrewlorenz changed the title AggregationCursor from collection has missing methods AggregationCursor from collection has missing method eachASync Apr 29, 2023
@andrewlorenz andrewlorenz changed the title AggregationCursor from collection has missing method eachASync AggregationCursor from collection is missing method eachASync Apr 29, 2023
@vkarpov15
Copy link
Collaborator

What indicates to you that const collA = await myCollection.aggregate([ { $match: { name: /test/i } ]); means collA is an AggregationCursor instance? await Model.aggregate() returns the results of the aggregation, not a cursor.

Here's how you get an aggregation cursor:

    const cursor = MyModel.
      aggregate([{ $match: { name: 'test' } }, { $project: { name: '$name' } }]).
      cursor();

    await cursor.eachAsync(doc => console.log(doc));

@vkarpov15 vkarpov15 removed this from the 7.1.2 milestone May 6, 2023
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed docs This issue is due to a mistake or omission in the mongoosejs.com documentation labels May 6, 2023
@andrewlorenz
Copy link
Author

I specifically said collection in my title and the description. I have clearly shown in my sample code that accessing via the model works, but NOT when accessing the collection directly. That is what my ticket was about.

@vkarpov15
Copy link
Collaborator

I'm sorry, I misread.

That's because collection.aggregate returns a MongoDB Node driver AggregationCursor instance, not a Mongoose AggregationCursor instance. Once you use collection., you bypass everything Mongoose specific, including the eachAsync() helper for cursors.

@andrewlorenz
Copy link
Author

Just to finish this off, and in case it helps anyone who hits this thread for info, the page https://mongoosejs.com/docs/api/connection.html#Connection.prototype.collection() documents that this method returns a "Collection" collection instance, and provides no further information, but advises to using a model instead.
There are however valid use cases for accessing collections directly (e.g. in my case, I am running only aggregates against collections and therefore don't want or need to include the models for those in the code), and in these cases you should refer instead to the mongodb node driver https://mongodb.github.io/node-mongodb-native

@vkarpov15 vkarpov15 reopened this May 11, 2023
@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels May 11, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.1.2, 7.1.3 May 11, 2023
vkarpov15 added a commit that referenced this issue May 30, 2023
docs(connection+model): expand docs on accessors for underlying collections
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants