AggregationCursor

AggregationCursor()

Parameters
  • agg «Aggregate»
  • options «Object»
Inherits:
  • «Readable»

An AggregationCursor is a concurrency primitive for processing aggregation results one document at a time. It is analogous to QueryCursor.

An AggregationCursor fulfills the Node.js streams3 API, in addition to several other mechanisms for loading documents from MongoDB one at a time.

Creating an AggregationCursor executes the model’s pre aggregate hooks, but not the model’s post aggregate hooks.

Unless you’re an advanced user, do not instantiate this class directly. Use Aggregate#cursor() instead.


AggregationCursor.prototype.Symbol.asyncIterator()

Returns an asyncIterator for use with [for/await/of loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js You do not need to call this function explicitly, the JavaScript runtime will call it for you.

Example

  1. // Async iterator without explicitly calling `cursor()`. Mongoose still
  2. // creates an AggregationCursor instance internally.
  3. const agg = Model.aggregate([{ $match: { age: { $gte: 25 } } }]);
  4. for await (const doc of agg) {
  5. console.log(doc.name);
  6. }
  7. // You can also use an AggregationCursor instance for async iteration
  8. const cursor = Model.aggregate([{ $match: { age: { $gte: 25 } } }]).cursor();
  9. for await (const doc of cursor) {
  10. console.log(doc.name);
  11. }

Node.js 10.x supports async iterators natively without any flags. You can enable async iterators in Node.js 8.x using the --harmony_async_iteration flag.

Note: This function is not set if Symbol.asyncIterator is undefined. If Symbol.asyncIterator is undefined, that means your Node.js version does not support async iterators.


AggregationCursor.prototype.addCursorFlag()

Parameters
  • flag «String»
  • value «Boolean»
Returns:
  • «AggregationCursor» this

Adds a cursor flag. Useful for setting the noCursorTimeout and tailable flags.


AggregationCursor.prototype.close()

Parameters
  • callback «Function»
Returns:
  • «Promise»

Marks this cursor as closed. Will stop streaming and subsequent calls to next() will error.


AggregationCursor.prototype.eachAsync()

Parameters
  • fn «Function»
  • [options] «Object»

  • [options.parallel] «Number» the number of promises to execute in parallel. Defaults to 1.

  • [callback] «Function» executed when all docs have been processed

Returns:
  • «Promise»

Execute fn for every document in the cursor. If fn returns a promise, will wait for the promise to resolve before iterating on to the next one. Returns a promise that resolves when done.


AggregationCursor.prototype.map()

Parameters
  • fn «Function»
Returns:
  • «AggregationCursor»

Registers a transform function which subsequently maps documents retrieved via the streams interface or .next()

Example

  1. // Map documents returned by `data` events
  2. Thing.
  3. find({ name: /^hello/ }).
  4. cursor().
  5. map(function (doc) {
  6. doc.foo = "bar";
  7. return doc;
  8. })
  9. on('data', function(doc) { console.log(doc.foo); });
  10. // Or map documents returned by `.next()`
  11. const cursor = Thing.find({ name: /^hello/ }).
  12. cursor().
  13. map(function (doc) {
  14. doc.foo = "bar";
  15. return doc;
  16. });
  17. cursor.next(function(error, doc) {
  18. console.log(doc.foo);
  19. });

AggregationCursor.prototype.next()

Parameters
  • callback «Function»
Returns:
  • «Promise»

Get the next document from this cursor. Will return null when there are no documents left.