mongo-modeling
Skill1 starsChanged 2 months agoMITView on GitHub
Model MongoDB documents for the access patterns — embed vs reference, the schema design patterns (subset, computed, bucket, outlier, extended reference), array pitfalls, index for the query shape, and the $lookup cost. Use when adding a Mongo collection, when asked to "design the document / schema", or when a Mongo query is slow or the documents are getting huge.
In this repository73 other files it gives its agents.
- AGENTS.mdAGENTS.md
- bigquery-analyticsSkill
- cache-invalidationSkill
- mongo-aggregationSkill
- 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
1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 13
Embed: when the sub-document:
≈ “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 modeling
Model for how the data is read. There is no "correct" normalized form — there is the shape that serves your queries with the fewest round-trips.
Embed vs reference
Embed when the sub-document:
- is (almost) always loaded with the parent,
- is bounded in size and count (not unbounded growth),
- doesn't need to be queried or updated independently at scale.
Reference when it:
- is large, unbounded, or high-cardinality (comments, events, audit log),
- is queried on its own,
- is shared by many parents,
- would push the parent toward the 16 MB document limit.
Design patterns
| Pattern | Use |
| Extended reference | copy the 2–3 fields of the referenced doc you always display, so the common read needs no $lookup (accept the denormalization; update on change) |
| Subset | embed the most recent / top N sub-docs, keep the full set in a referenced collection |
| Computed | store the rollup (counts, totals) on the parent, update on write — don't aggregate on every read |
| Bucket | for time-series / high-frequency inserts, one document per (device, hour) holding an array of readings, not one doc per reading |
| Outlier | most docs embed; the rare huge one flips to referenced with a flag |
Arrays
- Unbounded arrays are the #1 Mongo modeling mistake — they fragment, blow the doc-size limit, and make updates slow. Cap them (subset/bucket pattern).
$pushwith$sliceto keep an array bounded.- Multikey indexes on arrays: one array field per compound index (can't index two parallel arrays).
Indexing
- Index the query shape: equality fields first, then sort field, then range (same ESR rule as SQL composite indexes).
explain("executionStats")— checktotalDocsExaminedvsnReturned; large ratio = wrong or missing index.MONGO_AUTO_INDEX=falsein prod — build indexes deliberately, in the background.- Partial indexes (
partialFilterExpression) for "only active docs" queries. - TTL index for expiring data.
$lookup
$lookupis a nested loop — cheap for small driving sets and an indexed foreign field, expensive otherwise. If you$lookupon every read, you probably want an extended-reference embed instead.
Anti-patterns
- ❌ Normalizing like SQL and
$lookup-ing everything. - ❌ Unbounded embedded arrays.
- ❌ One document per high-frequency event (use the bucket pattern).
- ❌ Aggregating a count on every read instead of maintaining a computed field.
- ❌ Relying on
MONGO_AUTO_INDEXin prod.
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.

