Mongoose: AggregationCursor

Created on 23 Jun 2017  路  2Comments  路  Source: Automattic/mongoose

Related to https://github.com/Automattic/mongoose/issues/5145

Hi Guys, seems AggregationCursor misbehaves a lot.
If I add just a small option to cursor ({ useMongooseAggCursor: true }) then my previous code becomes broken. According to mongoDB documentation every cursor must have toArray method which is absent in AggregationCursor.

Also I haven't found useMongooseAggCursor in mongoose documentation.
And M.aggregate([{ $match: { name: 'test' } }]).cursor({ useMongooseAggCursor: true }).eachAsync() this code from https://github.com/Automattic/mongoose/issues/5145 doesn't work (Collection.aggregate(...).cursor(...).eachAsync is not a function)

docs

Most helpful comment

can you try:

const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

var Schema = mongoose.Schema;

mongoose.set('debug', true);

mongoose.connect('mongodb://localhost:27017/gh5145');

var schema = new Schema({});
var M = mongoose.model('Test', schema);

const cursor = M
  .aggregate([{ $match: { name: 'test' } }])
  .cursor({ batchSize: 1000, useMongooseAggCursor: true })
  .exec()
  .eachAsync(data => console.log(data))

All 2 comments

you need add options async: true . ex
` M.aggregate([{ $match: { name: 'test' } }]).allowDiskUse(true).cursor({ batchSize: 1000, async: true }).exec(function(error, cursor) {

                  cursor.eachAsync((data) => {

                        console.log ("-------------data-----------");
                              console.log(data);
                              console.log ("----------data--------------");   

                   }).then(() => console.log('done'));

              });`

can you try:

const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

var Schema = mongoose.Schema;

mongoose.set('debug', true);

mongoose.connect('mongodb://localhost:27017/gh5145');

var schema = new Schema({});
var M = mongoose.model('Test', schema);

const cursor = M
  .aggregate([{ $match: { name: 'test' } }])
  .cursor({ batchSize: 1000, useMongooseAggCursor: true })
  .exec()
  .eachAsync(data => console.log(data))

Was this page helpful?
0 / 5 - 0 ratings