agentleFS
tejastn10tejastn10/workbench/skills/data/mongo-aggregation/SKILL.md

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.

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
  1. Very specific:

    $sort before $group: if you want an index-backed sort.

    line 15
  2. Very specific:

    $limit early: when you only need top-N.

    line 19

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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.

  • $match as early as possible — filter before anything else.
  • $sort before $group if you want an index-backed sort; after $group it's an in-memory sort of the grouped output.
  • $project / $unset early to drop big fields you don't need downstream (less data through the pipeline).
  • $limit early when you only need top-N.

Stages

  • $group — _id is the group key; accumulators ($sum, $avg, $push, $addToSet, $first/$last with a prior $sort, $topN).
  • $lookup — join. Cheap only with an indexed foreignField and a small left side. $unwind after $lookup multiplies docs — filter first.
  • $facet — multiple sub-pipelines over the same input in one pass (counts + page + aggregates for a dashboard). Note: $facet can'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, not COLLSCAN).
  • Result doc still bound by 16 MB — a giant $group … $push can 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

  • ❌ $match after $group (index is already gone).
  • ❌ $lookup then $unwind then $match — filter before the join.
  • ❌ allowDiskUse: true as the fix instead of filtering earlier.
  • ❌ Running the full pipeline on every dashboard load.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.