最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mongoose aggregate cursor promise - Stack Overflow

programmeradmin2浏览0评论

I am trying to aggregate a large data set so I am using a cursor along with aggregate. However, I cannot find documentation on how to implement this without using an additional deferred. I feel there has to be a way to bine aggregate().cursor().each() with a promise that is resolved after aggregation is finished. Does anyone know how to do this?

This code works and is based on .html#aggregate_Aggregate-cursor I am trying to find a way to do this without the additional promise.

aggregation = MyModel.aggregate().group({
  _id: '$name'
});

deferred = Q.defer();

aggregation.cursor({
  batchSize: 1000
}).exec().each(function(err, doc) {
  if (err) {
    return deferred.reject(err);
  }
  if (!doc) {
    return deferred.resolve(); // done
  }
  // do stuff with doc
});
return deferred.promise;

I am trying to aggregate a large data set so I am using a cursor along with aggregate. However, I cannot find documentation on how to implement this without using an additional deferred. I feel there has to be a way to bine aggregate().cursor().each() with a promise that is resolved after aggregation is finished. Does anyone know how to do this?

This code works and is based on http://mongoosejs./docs/api.html#aggregate_Aggregate-cursor I am trying to find a way to do this without the additional promise.

aggregation = MyModel.aggregate().group({
  _id: '$name'
});

deferred = Q.defer();

aggregation.cursor({
  batchSize: 1000
}).exec().each(function(err, doc) {
  if (err) {
    return deferred.reject(err);
  }
  if (!doc) {
    return deferred.resolve(); // done
  }
  // do stuff with doc
});
return deferred.promise;

Share Improve this question asked May 4, 2016 at 0:44 saversaver 811 gold badge2 silver badges7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I found this SO looking for general help on using the aggregation cursor with promises. After a lot of trial and error and if anyone else stumbles on this, I found that the cursor object has a next() method that returns a promise, like other cursors. For some reason, I couldn't get a reference to it without the async flag, though. So if you're using bluebird:

let bluebird = require("bluebird");
let aggregation = MyModel.aggregate().group({
  _id: '$name'
});

aggregation.cursor({
  batchSize: 1000,
  async: true
}).exec().then(cursor => {
  return bluebird.coroutine(function* () {
    let doc;
    while ((doc = yield cursor.next())) {
      console.log(doc._id)
    }
  })()
}).then(() => { console.log("done with cursor"); })

Update for Mongoose 5

The exec() call no longer returns a promise, just the cursor itself, and the async: true property is no longer necessary.

let aggregation = MyModel.aggregate().group({
  _id: '$name'
});

(async function () {
  let doc, cursor;
  cursor = aggregation.cursor({batchSize: 1000}).exec();

  while ((doc = await cursor.next())) {
    console.log(doc._id)
  }
})()
.then(() => console.log("done with cursor"); )
发布评论

评论列表(0)

  1. 暂无评论