The aggregation pipeline can output results in three distinct ways:
- Inline as a document containing the result set
- In a collection
- Returning a cursor to the result set
Inline results are subject to the BSON maximum document size of 16 MB, meaning that we should use this only if our final result is of fixed size. An example of this would be outputting the ObjectIds of the top five most ordered items from an e-commerce site.
A contrary example to that would be outputting the top 1,000 ordered items, along with the product information, including the description and other fields of variable size.
Outputting results into a collection is the preferred solution if we want to perform further processing of data. We can either output into a new collection or replace the contents of an existing collection. The aggregation output results will only be visible once the aggregation...