Iterate through aggregation framework results

When using Mongo’s find command, one can iterate through result (using subsets) instead of getting the full set of documents. By using it it command will continue from current cursor.

With aggregation framework can one iterate through subsets of documents (from the set of documents returned by a query) and thus saving query solving and retrieving time and also memory?

Hi there.

In the mongo shell, if the cursor returned from the db.collection.aggregate() is not assigned to a variable using the var keyword, then the mongo shell automatically iterates the cursor up to 20 times. See Iterate a Cursor in the mongo Shell for handling cursors in the mongo shell.

Cursors returned from aggregation only supports cursor methods that operate on evaluated cursors (i.e. cursors whose first batch has been retrieved), such as the following methods:

In answer to your question, it is possible to iterate over a set of results once the cursor is assigned to a variable.