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

Aggregate cursor is undefined #3160

Closed
TimonKK opened this issue Jul 14, 2015 · 10 comments
Closed

Aggregate cursor is undefined #3160

TimonKK opened this issue Jul 14, 2015 · 10 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@TimonKK
Copy link

TimonKK commented Jul 14, 2015

I am trying this code:

var cursor = MyModel
    .aggregate([
        {
            $match: { 
                mark : "some mark" 
            }
        }
    ])
    .cursor({ batchSize: 1000 })
    .exec();

cursor.each(function(error, doc) {
  console.log('cursor each', error, doc);
});

from Mongoose API
But i have

cursor.each(function(error, doc) {
      ^
TypeError: Cannot read property 'each' of undefined

I am using

node v.0.11.14
MongoDB version 2.6.6
mongoose 4.0.5
@vkarpov15
Copy link
Collaborator

One of the unfortunate quirks of the .aggregate() implementation right now is that it doesn't work with buffering, so you need to do

connection.on('connected', function() {

});

to wait for mongoose to connect to the db before using the aggregation cursor :( This is something that will be improved in the future, but unfortunately something you need to work around for now.

@vkarpov15 vkarpov15 added this to the 4.2 milestone Jul 14, 2015
@TimonKK
Copy link
Author

TimonKK commented Jul 14, 2015

This does not work.
If place my code in callback "mongoose.connect" - it start work.
But, if i do:

var cursor = MyModel
  .aggregate([
    {
        $match: { 
          mark : "some mark" 
        }
    }
  ])
  .cursor({ batchSize: 1000 })
  .exec(function() {
    // never cause
  });

cursor
  .on('error', function(err) {

  })
  .on('data', function(data) {

  })
  .on('end', function() {

  });

it is work too.

@vkarpov15
Copy link
Collaborator

Yeah another way to get it to work is in the mongoose.connect() callback. Aggregation cursor unfortunately doesn't work with buffering, so it will only start working once mongoose is connected to the db.

@timjnh
Copy link

timjnh commented Jul 23, 2015

Confirmed TimonKK's issue with on('connected') not working. This does not work with mongoose 4.0.8 and mongo 3.0.3

var schema = new mongoose.Schema(
    {
        account:  { id: Number },
        source: { id: Number },
    });

var DocumentView = mongoose.model('DocumentView', schema);

var connection = mongoose.createConnection('mongodb://localhost:27017/reporting');
connection.on('connected', function() {
    var cursor = DocumentView.aggregate()
        .allowDiskUse(true)
        .group({ _id: { sourceId: '$source.id', accountId: '$account.id' }, count: { $sum: 1 } })
        .cursor({ batchSize: 1000 })
        .exec();

    console.log(cursor);
});

cursor will be undefined. However, if you use the mongoose.connect callback it does work:

mongoose.connect('mongodb://localhost:27017/reporting', function() {

    var cursor = DocumentView.aggregate()
        .allowDiskUse(true)
        .group({ _id: { sourceId: '$source.id', accountId: '$account.id' }, count: { $sum: 1 } })
        .cursor({ batchSize: 1000 })
        .exec();

    console.log(cursor);
});

vkarpov15 added a commit that referenced this issue Oct 8, 2015

Verified

This commit was signed with the committer’s verified signature.
@ghost
Copy link

ghost commented Apr 25, 2016

When adding the async cursor option I get the following error:

MongoError: cursor object can't contain fields other than batchSize

I am using:

NodeJS 4.2.4
MongoDB version 3.2.5
Mongoose 4.4.13

@vkarpov15
Copy link
Collaborator

@raoulwegat can you open up a separate issue with a code sample please?

@ghost
Copy link

ghost commented Apr 27, 2016

Yes of course, here you go: #4101.

@vkarpov15
Copy link
Collaborator

Thanks, much appreciated

@mhombach
Copy link

mhombach commented Sep 2, 2020

I think the label "pending release" is incorrect because this was already released to my understanding?

@vkarpov15
Copy link
Collaborator

@mhombach yep that's incorrect, I'll fix that.

@vkarpov15 vkarpov15 added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature and removed pending release labels Sep 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

4 participants