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

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.

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
  1. Distinctive:

    Embed: when the sub-document:

    line 13

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

PatternUse
Extended referencecopy the 2–3 fields of the referenced doc you always display, so the common read needs no $lookup (accept the denormalization; update on change)
Subsetembed the most recent / top N sub-docs, keep the full set in a referenced collection
Computedstore the rollup (counts, totals) on the parent, update on write — don't aggregate on every read
Bucketfor time-series / high-frequency inserts, one document per (device, hour) holding an array of readings, not one doc per reading
Outliermost 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).
  • $push with $slice to 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") — check totalDocsExamined vs nReturned; large ratio = wrong or missing index.
  • MONGO_AUTO_INDEX=false in prod — build indexes deliberately, in the background.
  • Partial indexes (partialFilterExpression) for "only active docs" queries.
  • TTL index for expiring data.

$lookup

  • $lookup is a nested loop — cheap for small driving sets and an indexed foreign field, expensive otherwise. If you $lookup on 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_INDEX in prod.

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.