Quick Start with MongoDB Aggregation: Detailed Explanation of $match and $group Operators
The MongoDB aggregation pipeline is a data processing pipeline composed of multiple stages (operators) that can filter, count, and transform data sequentially. This article focuses on the two most commonly used operators: `$match` and `$group`. `$match`, similar to the SQL `WHERE` clause, filters documents that meet a specified condition. Its syntax is `{ $match: { query conditions } }`, supporting operations such as equality, greater than, less than, and inclusion (e.g., `class: "Class 1"` or `score: { $gt: 80 }`). In the example, students in "Class 1" are filtered, returning 3 documents. `$group` groups documents by a field and performs statistics. The syntax is `{ $group: { _id: grouping key, custom field: { accumulator: field name } } }`, where accumulators include `$sum` (sum), `$avg` (average), and `$count` (count). Examples include: counting students by class (3 in Class 1, 2 in Class 2), summing total scores by subject (256 for Math, 177 for Chinese), and calculating average scores by class. These two operators are often used together, e.g., first filtering documents for the Math subject, then calculating average scores by class. In summary, `$match` acts as a filter, `$group` as a calculator, and their combination is the core pattern of aggregation analysis. Subsequent extensions (e.g., `$project` for data projection) can be explored.
Read More