Final answer:
The $match stage in MongoDB aggregation is placed early to filter documents based on criteria before further calculations. It improves performance by reducing the number of documents in subsequent stages.
Step-by-step explanation:
When performing aggregation on a subset of documents in MongoDB, the $match stage is placed early in the aggregation pipeline. This stage is used to filter the documents based on certain criteria before further transformations and calculations are applied.
By placing the $match stage early, it reduces the number of documents that need to be processed in the subsequent stages, improving the overall performance of the aggregation process.
For example, if you are calculating the average age of male students, you can start with the $match stage to filter out only the male students based on their gender field, reducing the number of documents for further calculations.