mongo-aggregation
Skill1 starsChanged 2 months agoMITView on GitHub
Write and review MongoDB aggregation pipelines — stage order for index use ($match/$sort first), $group, $lookup and $unwind cost, $facet, memory limits and allowDiskUse, and when to use a materialized rollup instead. Use when building a report/analytics pipeline in Mongo, aggregating documents, or when a pipeline is slow or hits the memory limit.
In this repository73 other files it gives its agents.
- AGENTS.mdAGENTS.md
- bigquery-analyticsSkill
- cache-invalidationSkill
- mongo-modelingSkill
- postgres-aggregationSkill
- postgres-indexingSkill
- redis-coordinationSkill
- redis-patternsSkill
- review-querySkill
- code-migrationSkill
- cut-releaseSkill
- data-migrationSkill
- deploy-serviceSkill
- hotfixSkill
- rollbackSkill
- design-endpointSkill
- design-eventSkill
- design-schemaSkill
- evolve-contractSkill
- scaffold-nestjs-moduleSkill
- scaffold-projectSkill
- incident-responseSkill
- investigate-bugSkill
- on-callSkill
- postmortemSkill
- audit-skillsSkill
- distill-review-styleSkill
- write-skillSkill
- debug-with-tracesSkill
- define-alertsSkill
- instrument-serviceSkill
- evaluate-dependencySkill
- grillSkill
- handoffSkill
- orientSkill
- phased-deliverySkill
- spikeSkill
- to-questionnaireSkill
- write-adrSkill
- write-prdSkill
- devops-pr-reviewSkill
- go-backend-pr-reviewSkill
- nestjs-backend-pr-reviewSkill
- python-backend-pr-reviewSkill
- react-frontend-pr-reviewSkill
- bisectSkill
- break-couplingSkill
- dead-code-sweepSkill
- deslopifySkill
- extract-moduleSkill
- rebase-cleanSkill
- resolve-merge-conflictsSkill
- split-commitSkill
- tddSkill
- wait-whatSkill
- wide-renameSkill
- bullmqSkill
- kafkaSkill
- sqs-consumerSkill
- audit-dependenciesSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 15
$sortbefore$group: if you want an index-backed sort. - Very specific: line 19
$limitearly: when you only need top-N.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
MongoDB aggregation
Stage order is performance
The pipeline runs top to bottom. Only the leading $match and $sort can use
an index — once a $group / $project / $unwind transforms the docs, later
stages scan in memory.
$matchas early as possible — filter before anything else.$sortbefore$groupif you want an index-backed sort; after$groupit's an in-memory sort of the grouped output.$project/$unsetearly to drop big fields you don't need downstream (less data through the pipeline).$limitearly when you only need top-N.
Stages
$group—_idis the group key; accumulators ($sum,$avg,$push,$addToSet,$first/$lastwith a prior$sort,$topN).$lookup— join. Cheap only with an indexedforeignFieldand a small left side.$unwindafter$lookupmultiplies docs — filter first.$facet— multiple sub-pipelines over the same input in one pass (counts + page + aggregates for a dashboard). Note:$facetcan't use indexes for its sub-pipelines beyond what the pre-facet stages established.$setWindowFields— running totals / rank / lag, the window-function equivalent.$merge/$out— write the result to a collection (materialized rollup).
Limits
- Each stage has a 100 MB memory cap. Hitting it →
{ allowDiskUse: true }, but that's a smell: filter earlier, or pre-aggregate. explain("executionStats")on the pipeline — check the leading stage uses an index (IXSCAN, notCOLLSCAN).- Result doc still bound by 16 MB — a giant
$group … $pushcan exceed it.
When not to use a pipeline
Dashboards that load constantly should read a rollup collection maintained
incrementally ($merge on a schedule, or updated on write via the computed
pattern), not run a full aggregation every request — same principle as Postgres
materialized views (data/postgres-aggregation).
Anti-patterns
- ❌
$matchafter$group(index is already gone). - ❌
$lookupthen$unwindthen$match— filter before the join. - ❌
allowDiskUse: trueas the fix instead of filtering earlier. - ❌ Running the full pipeline on every dashboard load.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

