agentleFS
Lukk17Lukk17/agent-standards/.agents/skills/mongodb-patterns/SKILL.md

mongodb-patterns

Skill1 starsChanged 57 days agoMITView on GitHub

MongoDB across connection pooling, query optimisation and indexing, schema design and anti-patterns, and Atlas Search, Vector Search and Hybrid Search. Use when you say \"configure the connection pool\", \"why do I get ECONNREFUSED or pool exhaustion\", \"how do I optimize this query\", \"how do I index this\", \"what are the slow queries on my cluster\", \"design this schema\", \"embed vs reference\", \"unbounded arrays\", \"16MB limit\", \"schema validation\", \"time series\", \"polymorphic\", \"document versioning\", \"add autocomplete or fuzzy matching\", \"build RAG over this collection\", or \"combine keyword and semantic search\". Not for applying a schema change safely to a live cluster, use `database-migrations`.

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 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.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Every area here fails the same way: a value applied without knowing the workload.

    line 55
  2. Distinctive:

    Ask one question at a time, broad before specific, and when an answer never arrives make a reasonable assumption and say out loud that you …

    line 57
  3. Very specific:

    Run the MCP server with --readOnly unless writes are genuinely needed.

    line 82

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 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 Patterns

Connection configuration, query and index tuning, data modelling, and search, with a reference file per area. The hub carries the rules that apply to every MongoDB task, and each reference carries the depth for one of them.


Baseline

Assume MongoDB 8.0 or newer on Atlas, and an officially supported driver (Node.js, Python, Java, Go, C#, Ruby, PHP). Four facts change what the right answer is, so establish them before advising anything:

  • The deployment shape. Serverless functions, a long-running server, and an analytical job need different pools.
  • The topology. Pools are per client and per server, so a replica set or a sharded cluster multiplies the total.
  • The server version. MongoDB 8.0 adds defaultMaxTimeMS on Atlas, $rankFusion needs 8.0 and $scoreFusion 8.2.
  • The workload. Read and write access patterns decide the document model, the indexes, and the pool size alike.

When to activate

  • Instantiating or configuring a MongoDB client, a connection pool, or driver timeouts.
  • Diagnosing ECONNREFUSED, a socket timeout, pool exhaustion, or connection churn.
  • Asking why a query is slow, which index it needs, or what the slow queries on a cluster are.
  • Designing a new schema, reviewing an existing one, or migrating a relational model into documents.
  • Hitting the 16MB document limit, an unbounded array, or an Atlas Schema Suggestion.
  • Building full-text search, autocomplete, fuzzy matching, faceted filtering, or semantic and hybrid search.
  • Adding embeddings and retrieval for a RAG application on top of a collection.

When not to activate

  • Applying a schema or index change safely to a live production cluster. Use database-migrations.
  • Relational modelling, indexing, and tuning on PostgreSQL. Use postgres-patterns.
  • Idempotency, retries, and graceful shutdown in the service around the driver. Use backend-patterns.
  • Measuring the endpoint before assuming the database is the bottleneck. Use performance-optimization.
  • Language-neutral design rules such as SOLID, DRY, and error-handling shape. Use coding-standards.
  • Reviewing a diff or a pull request for defects. Use code-reviewer.

Gather context before recommending anything

Every area here fails the same way: a value applied without knowing the workload. A maxPoolSize copied from a blog post, an index added because a field appeared in a filter, an embedded array chosen because the SQL table was joined. Ask one question at a time, broad before specific, and when an answer never arrives make a reasonable assumption and say out loud that you assumed it.

GOOD: maxPoolSize 50, from your observed peak of 40 concurrent operations plus 25 percent headroom.
BAD:  maxPoolSize 100, because that is the default people use.

Inspect the real cluster before guessing at it

The MongoDB MCP server turns most of this skill from advice into measurement. Use collection-schema for the real field structure, collection-indexes for what already exists, explain for the plan, db-stats for size, and atlas-get-performance-advisor for slow query logs and index suggestions. When neither the connection string nor the Atlas API is available, say so plainly and reason from the query shape instead of pretending to have measured.


Never run a write through MCP without explicit approval

Reads such as find, aggregate, collection-schema, db-stats and count are safe to run to verify a claim. Anything that changes state (create-index, update-many, insert-many, create-collection) and anything destructive (delete-many, drop-collection, drop-database) gets the same treatment first: state the exact operation, name the collection and the estimated number of documents affected, then wait for an unambiguous yes. Run the MCP server with --readOnly unless writes are genuinely needed.


Data that is accessed together should be stored together

This is the one modelling rule the rest derive from. Design documents around the queries an endpoint actually runs, not around the entities a relational schema happened to have. Embed when data is read and written together and the array is bounded, reference when the sides are accessed independently, the relationship is many to many, or the array can grow without limit. Queries and indexes cannot rescue a model that fights its own access pattern.


Prefer an index over a rewrite, and order it by ESR

Most slow queries are an index problem, so reach for indexing first and rewrite the query second. Order a compound index equality fields first, then the sort field, then the range fields. Aim for an index that covers the query so the plan never touches the documents. Keep the count per collection sensible, generally under 20, because every index costs write throughput and memory.


Never use $regex or $text for search

Neither scales as a search feature. $regex has no relevance scoring, no fuzzy matching, and no language-aware tokenization, and an unanchored pattern cannot use an index. $text is a legacy operator that does not hold up under a real search workload. When a user asks for either, explain why and show the Atlas Search equivalent.


Create the client once and reuse it

A MongoClient owns the pool, so constructing one per request destroys the reason pooling exists. In a serverless runtime, build it outside the handler so warm invocations reuse it. Do not close connections manually except at shutdown. Account for the monitoring connections when planning capacity: the total is roughly (maxPoolSize + 2) x replica set members x application instances.


Which reference to open for which task

Each entry below lists the deeper files for its own area, so open the entry first and let it route you further.

TaskReference
Pool sizing, timeouts, serverless reuse, connection errors, churn, pool monitoringreferences/connection.md
Slow queries, explain output, index choice, aggregation tuning, Performance Advisorreferences/query-optimizer.md
Embed versus reference, document model, validation, the 16MB limit, design patternsreferences/schema-design.md
Atlas Search, autocomplete and fuzzy matching, Vector Search, RAG, hybrid searchreferences/search-and-ai.md

Related skills

  • database-migrations for applying a schema or index change to a cluster that is already serving traffic.
  • postgres-patterns for the relational half of a system that also runs MongoDB.
  • backend-patterns for timeouts, retries, caching, and pagination in the service around the driver.
  • performance-optimization for measuring the request before blaming the database.
  • coding-standards for the cross-language engineering floor these patterns sit on.
  • code-reviewer for reviewing a change against all of the above.

Checklist

  • The deployment shape, topology, server version, and workload are known or the assumption is stated.
  • The client is constructed once and reused, and in serverless it lives outside the handler.
  • Pool and timeout values each have a reason attached, and the server-side connection total was calculated.
  • Existing indexes and the real schema were inspected before a new index or model was proposed.
  • Compound indexes follow equality, sort, range, and the collection is not carrying dead indexes.
  • The document model follows the access pattern, with no unbounded array and no document near 16MB.
  • Search uses Atlas Search or Vector Search, never $regex or $text.
  • Hybrid search was version checked before $rankFusion or $scoreFusion was suggested.
  • No write, index creation, or destructive MCP call ran without explicit approval.

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.