The distinct operator will get all unique values for that field within the collection, so something like:
db.getCollection('Books').distinct('Authors')
Will return the distinct list of authors over all documents within the Books collection.
For your case, unless I’m just misunderstanding your question you just want to process data as pulled back, you should note that if order is important you’re not supplying a sort criteria, within the aggregate this can be done via $sort stage.
Other than that the node driver will be returning a cursor to the docs, if you want to loop over it, take a look at the docs:
Here is a linked sample project, looping over results of an aggregation query in node:
1 Like