MongoDB Aggregation
Builds complex MongoDB aggregation pipelines for data transformation, analysis, and reporting. Covers all major pipeline stages including $match, $group, $lookup (joins), $unwind, $project, $addFields, $facet for parallel pipelines, $bucket for histograms, $merge/$out for materialized views, window functions, and pipeline optimization with index utilization and explain analysis.
Usage
Describe the data transformation or analysis you need to perform, the collection schema, and the expected output format. Specify the MongoDB version for feature availability. Provide sample documents if possible. The skill builds an optimized aggregation pipeline with explanations for each stage.
Examples
- "Calculate monthly revenue by product category with year-over-year comparison from an orders collection"
- "Join users and orders collections, then compute each user's total spend and order count for the last 90 days"
- "Build a faceted search pipeline that returns results, category counts, price range histogram, and total count simultaneously"
- "Create a pipeline that detects duplicate records based on email field and keeps only the most recent"
Guidelines
- Place $match stages as early as possible in the pipeline to reduce documents processed by later stages
- Ensure $match at the start of the pipeline can use indexes — check with explain('executionStats')
- Use $project or $addFields to reduce document size early, dropping unnecessary fields before $group
- Prefer $lookup with pipeline syntax over simple $lookup for filtered joins and better performance
- Use $facet for multiple aggregations in a single query instead of running separate queries
- Set allowDiskUse: true for pipelines that exceed 100MB memory limit, but optimize first
- Use $merge to write aggregation results to a collection for materialized views that update incrementally
- Break extremely complex pipelines into smaller, testable stages and compose them programmatically