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`.
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.
Task
Reference
Pool sizing, timeouts, serverless reuse, connection errors, churn, pool monitoring
references/connection.md
Slow queries, explain output, index choice, aggregation tuning, Performance Advisor
references/query-optimizer.md
Embed versus reference, document model, validation, the 16MB limit, design patterns
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.
1---2name: mongodb-patterns3description: 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`.4license: Apache-2.05---67# MongoDB Patterns89Connection configuration, query and index tuning, data modelling, and search, with a reference file per area. The hub10carries the rules that apply to every MongoDB task, and each reference carries the depth for one of them.1112---1314### Baseline1516Assume MongoDB 8.0 or newer on Atlas, and an officially supported driver (Node.js, Python, Java, Go, C#, Ruby, PHP).17Four facts change what the right answer is, so establish them before advising anything:1819- The deployment shape. Serverless functions, a long-running server, and an analytical job need different pools.20- The topology. Pools are per client and per server, so a replica set or a sharded cluster multiplies the total.21- The server version. MongoDB 8.0 adds `defaultMaxTimeMS` on Atlas, `$rankFusion` needs 8.0 and `$scoreFusion` 8.2.22- The workload. Read and write access patterns decide the document model, the indexes, and the pool size alike.2324---2526### When to activate2728- Instantiating or configuring a MongoDB client, a connection pool, or driver timeouts.29- Diagnosing `ECONNREFUSED`, a socket timeout, pool exhaustion, or connection churn.30- Asking why a query is slow, which index it needs, or what the slow queries on a cluster are.31- Designing a new schema, reviewing an existing one, or migrating a relational model into documents.32- Hitting the 16MB document limit, an unbounded array, or an Atlas Schema Suggestion.33- Building full-text search, autocomplete, fuzzy matching, faceted filtering, or semantic and hybrid search.34- Adding embeddings and retrieval for a RAG application on top of a collection.3536---3738### When not to activate3940- Applying a schema or index change safely to a live production cluster. Use `database-migrations`.41- Relational modelling, indexing, and tuning on PostgreSQL. Use `postgres-patterns`.42- Idempotency, retries, and graceful shutdown in the service around the driver. Use `backend-patterns`.43- Measuring the endpoint before assuming the database is the bottleneck. Use `performance-optimization`.44- Language-neutral design rules such as SOLID, DRY, and error-handling shape. Use `coding-standards`.45- Reviewing a diff or a pull request for defects. Use `code-reviewer`.4647---4849### Gather context before recommending anything5051Every area here fails the same way: a value applied without knowing the workload. A `maxPoolSize` copied from a blog52post, an index added because a field appeared in a filter, an embedded array chosen because the SQL table was joined.53Ask one question at a time, broad before specific, and when an answer never arrives make a reasonable assumption and54say out loud that you assumed it.5556```text57GOOD: maxPoolSize 50, from your observed peak of 40 concurrent operations plus 25 percent headroom.58BAD: maxPoolSize 100, because that is the default people use.59```6061---6263### Inspect the real cluster before guessing at it6465The MongoDB MCP server turns most of this skill from advice into measurement. Use `collection-schema` for the real66field structure, `collection-indexes` for what already exists, `explain` for the plan, `db-stats` for size, and67`atlas-get-performance-advisor` for slow query logs and index suggestions. When neither the connection string nor the68Atlas API is available, say so plainly and reason from the query shape instead of pretending to have measured.6970---7172### Never run a write through MCP without explicit approval7374Reads such as `find`, `aggregate`, `collection-schema`, `db-stats` and `count` are safe to run to verify a claim.75Anything that changes state (`create-index`, `update-many`, `insert-many`, `create-collection`) and anything76destructive (`delete-many`, `drop-collection`, `drop-database`) gets the same treatment first: state the exact77operation, name the collection and the estimated number of documents affected, then wait for an unambiguous yes.78Run the MCP server with `--readOnly` unless writes are genuinely needed.7980---8182### Data that is accessed together should be stored together8384This is the one modelling rule the rest derive from. Design documents around the queries an endpoint actually runs,85not around the entities a relational schema happened to have. Embed when data is read and written together and the86array is bounded, reference when the sides are accessed independently, the relationship is many to many, or the array87can grow without limit. Queries and indexes cannot rescue a model that fights its own access pattern.8889---9091### Prefer an index over a rewrite, and order it by ESR9293Most slow queries are an index problem, so reach for indexing first and rewrite the query second. Order a compound94index equality fields first, then the sort field, then the range fields. Aim for an index that covers the query so95the plan never touches the documents. Keep the count per collection sensible, generally under 20, because every index96costs write throughput and memory.9798---99100### Never use $regex or $text for search101102Neither scales as a search feature. `$regex` has no relevance scoring, no fuzzy matching, and no language-aware103tokenization, and an unanchored pattern cannot use an index. `$text` is a legacy operator that does not hold up under104a real search workload. When a user asks for either, explain why and show the Atlas Search equivalent.105106---107108### Create the client once and reuse it109110A `MongoClient` owns the pool, so constructing one per request destroys the reason pooling exists. In a serverless111runtime, build it outside the handler so warm invocations reuse it. Do not close connections manually except at112shutdown. Account for the monitoring connections when planning capacity: the total is roughly113`(maxPoolSize + 2) x replica set members x application instances`.114115---116117### Which reference to open for which task118119Each entry below lists the deeper files for its own area, so open the entry first and let it route you further.120121| Task | Reference |122| --- | --- |123| Pool sizing, timeouts, serverless reuse, connection errors, churn, pool monitoring | [references/connection.md](references/connection.md) |124| Slow queries, `explain` output, index choice, aggregation tuning, Performance Advisor | [references/query-optimizer.md](references/query-optimizer.md) |125| Embed versus reference, document model, validation, the 16MB limit, design patterns | [references/schema-design.md](references/schema-design.md) |126| Atlas Search, autocomplete and fuzzy matching, Vector Search, RAG, hybrid search | [references/search-and-ai.md](references/search-and-ai.md) |127128---129130### Related skills131132- `database-migrations` for applying a schema or index change to a cluster that is already serving traffic.133- `postgres-patterns` for the relational half of a system that also runs MongoDB.134- `backend-patterns` for timeouts, retries, caching, and pagination in the service around the driver.135- `performance-optimization` for measuring the request before blaming the database.136- `coding-standards` for the cross-language engineering floor these patterns sit on.137- `code-reviewer` for reviewing a change against all of the above.138139---140141### Checklist142143- [ ] The deployment shape, topology, server version, and workload are known or the assumption is stated.144- [ ] The client is constructed once and reused, and in serverless it lives outside the handler.145- [ ] Pool and timeout values each have a reason attached, and the server-side connection total was calculated.146- [ ] Existing indexes and the real schema were inspected before a new index or model was proposed.147- [ ] Compound indexes follow equality, sort, range, and the collection is not carrying dead indexes.148- [ ] The document model follows the access pattern, with no unbounded array and no document near 16MB.149- [ ] Search uses Atlas Search or Vector Search, never `$regex` or `$text`.150- [ ] Hybrid search was version checked before `$rankFusion` or `$scoreFusion` was suggested.151- [ ] No write, index creation, or destructive MCP call ran without explicit approval.
Run npx skillmds@latest add lukk17/mongodb-patterns in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
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`. It is listed under AI & ML on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free. This skill is licensed under Apache-2.
Lukk17 (@lukk17) published this skill. Their other Agent Skills are listed on their SkillMD profile.