The arguments to the db.<collection_name>.aggregate() method consist of a series of expressions, each beginning with a pipeline stage operator (see the next section). The output from one pipeline stage feeds into the next, in a chain, until the final results are returned. The following diagram shows the generic syntax:
Arguably, the most popular pipeline stage operators are $match, which performs a similar function to a query filter, and $group, which groups results according to the _id field specified. As a quick example, before getting into more details, note the following command, which produces a revenue report by country.
The $match stage allows documents where paymentStatus in the bookingInfo embedded object is confirmed. The $group stage performs grouping by country. In addition, the $group stage uses the $sum pipeline expression operator...