<!-- llms-explorer concept facts · https://llms-explorer.com/tree/mongodb-developer-patterns/ · pack 2026-09-08 · ~8939 tokens -->

# MongoDB Developer Patterns

> This local skill is generated from docs/mongodb-developer-context.md.

Parent: [MongoDB Expert Knowledge](https://llms-explorer.com/tree/mongodb-expert-knowledge/) · 40 facets · 181 facts · page: https://llms-explorer.com/tree/mongodb-developer-patterns/

## MongoDB Developer Context

- This local skill is generated from docs/mongodb-developer-context.md. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#mongodb-developer-context`

## When to use this skill

- Use this skill when the user needs help with: — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Writing code using any MongoDB official driver (Node.js, Python, Java, Go, C#, Rust, PHP, Ruby, Kotlin, Scala, C, C++) — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Connection string construction, pooling configuration, and topology events — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Error handling, retry logic, and resilient application patterns — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Multi-document transactions and causal consistency — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Bulk write operations, ordered and unordered — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Aggregation pipelines from driver code — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Change streams from driver code — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - GridFS file storage and retrieval — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - mongosh commands, scripting, and automation — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - MongoDB error codes and their resolutions — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Atlas Admin API calls — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Atlas MCP server tools — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Schema design, index strategy, aggregation patterns — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Antipatterns and common failure modes — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
  - Customer troubleshooting (slow queries, connection issues, auth failures, replica set elections) — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`
- Start from the bundled context below, and defer to the cited official documentation for exact APIs, commands, and edge-case behavior. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-this-skill`

## Skill guidance

- Treat docs/mongodb-developer-context.md as the source document for this skill. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#skill-guidance`
- Prefer the workflows, checklists, and patterns captured in the bundled context before improvising. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#skill-guidance`
- Cross-reference with mongodb-expert skill for general MQL/aggregation depth. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#skill-guidance`
- Cross-reference with mongodb-atlas-expert skill for Atlas-specific operational depth. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#skill-guidance`
- Cross-reference with mongodb-performance-troubleshooting skill for deep performance analysis. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#skill-guidance`
- Cross-reference with mongodb-schema-design skill for data modeling patterns. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#skill-guidance`
- Cross-reference with mongodb-data-lifecycle skill for change streams and TTL details. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#skill-guidance`
- Cross-reference with mongodb-encryption skill for CSFLE and Queryable Encryption. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#skill-guidance`
- If the request is outside this topic, choose a more appropriate skill instead of forcing this one. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#skill-guidance`

## SRV Connection String (Atlas and DNS seedlist)

- SRV records provide automatic host discovery and TLS defaults. Atlas always provides SRV URIs. The driver resolves DNS SRV and TXT records to discover all mongos/replica set members. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#srv-connection-string-atlas-and-dns-seedlist`

## Connection String Best Practices

- Always set appName so ops teams can trace connections in server logs. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#connection-string-best-practices`
- Use SRV connection strings for Atlas and any DNS-seedlist deployment. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#connection-string-best-practices`
- Never hard-code credentials; use environment variables or a secrets manager. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#connection-string-best-practices`
- Set compressors=zstd for bandwidth-sensitive workloads (requires server and driver support). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#connection-string-best-practices`
- For serverless functions (Lambda, Cloud Functions), set maxPoolSize=1 and maxIdleTimeMS=10000 to avoid connection exhaustion. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#connection-string-best-practices`
- Set retryWrites=true&retryReads=true explicitly in shared URIs for clarity (both are default since 4.2). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#connection-string-best-practices`

## How Pools Work

- Each MongoClient maintains a pool of TCP connections per server (per replica set member or mongos). When your application requests an operation, the driver checks out a connection from the pool, executes the operation, and returns the connection. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#how-pools-work`

## The Golden Rule: One Client Per Application

- Create a single MongoClient instance and share it across your application. The client is thread-safe (or goroutine-safe, or async-safe) in every official driver. Opening a new client per request is the most common pooling antipattern. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#the-golden-rule-one-client-per-application`

## Pool Monitoring Events

- All drivers emit connection pool events for observability: — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#pool-monitoring-events`
  - connectionPoolCreated / connectionPoolClosed — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#pool-monitoring-events`
  - connectionCreated / connectionClosed — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#pool-monitoring-events`
  - connectionCheckedOut / connectionCheckedIn — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#pool-monitoring-events`
  - connectionCheckOutFailed / connectionCheckOutStarted — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#pool-monitoring-events`
  - connectionPoolCleared — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#pool-monitoring-events`
- Subscribe to these events to track pool saturation, connection churn, and wait-queue depth. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#pool-monitoring-events`

## 3.1 Node.js (mongodb package)

- Installation: npm install mongodb — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#31-nodejs-mongodb-package`
- Singleton Pattern: — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#31-nodejs-mongodb-package`
- Graceful Shutdown: — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#31-nodejs-mongodb-package`

## 3.2 Python (PyMongo)

- Installation: pip install pymongo[srv] — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#32-python-pymongo`
- Singleton Pattern: — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#32-python-pymongo`
- Motor (Async PyMongo): — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#32-python-pymongo`

## 3.3 Java (mongodb-driver-sync / mongodb-driver-reactivestreams)

- Maven Dependency (sync driver): — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#33-java-mongodb-driver-sync-mongodb-driver-reactivestreams`
- Spring Data MongoDB Integration: — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#33-java-mongodb-driver-sync-mongodb-driver-reactivestreams`

## 3.4 Go (go.mongodb.org/mongo-driver v2)

- Installation: go get go.mongodb.org/mongo-driver/v2/mongo — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#34-go-gomongodborgmongo-driver-v2`

## 3.5 C# (.NET Driver)

- NuGet: MongoDB.Driver — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#35-c-net-driver`

## Retryable Writes

- Enabled by default since MongoDB 4.2. The driver automatically retries eligible write operations exactly once after a transient network error or a failover. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-writes`
- Eligible operations: insertOne, updateOne, replaceOne, deleteOne, findOneAndUpdate, findOneAndReplace, findOneAndDelete, insertMany (ordered or unordered), bulkWrite (ordered or unordered). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-writes`
  - updateMany, deleteMany (not idempotent at the protocol level) — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-writes`
  - Writes with w: 0 (unacknowledged) — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-writes`
  - Individual writes within an explicit transaction (the transaction itself is retried) — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-writes`
  - RetryableWriteError - the driver retries automatically — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-writes`
  - NoWritesPerformed (MongoDB 6.1+) - both attempts failed without writing; safe to retry at app layer — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-writes`
  - TransientTransactionError - retry the entire transaction — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-writes`
  - UnknownTransactionCommitResult - retry commitTransaction() — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-writes`

## Retryable Reads

- Enabled by default since MongoDB 4.2. The driver retries eligible read operations exactly once after transient network errors. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-reads`
- Eligible operations: find, findOne, aggregate (without $out/$merge), distinct, count, estimatedDocumentCount, listDatabases, listCollections, listIndexes. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-reads`

## Error Handling Strategy Checklist

- Let the driver handle retryable errors (retryWrites/retryReads). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#error-handling-strategy-checklist`
- Catch DuplicateKeyError (11000) for idempotent upserts. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#error-handling-strategy-checklist`
- Catch ServerSelectionTimeoutError for connectivity failures and alert. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#error-handling-strategy-checklist`
- Catch WriteConcernError when w: majority cannot be satisfied. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#error-handling-strategy-checklist`
- Catch MaxTimeMSExpired (50) and investigate slow queries. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#error-handling-strategy-checklist`
- Wrap bulk operations to inspect BulkWriteError.writeErrors array. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#error-handling-strategy-checklist`
- Log error codes, not just messages, for searchability. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#error-handling-strategy-checklist`
- Never swallow errors silently; always log or propagate. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#error-handling-strategy-checklist`

## When to Use

- Use transactions when business logic requires atomic updates across multiple documents or collections. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use`
- Prefer single-document atomicity when possible; redesign schemas before reaching for transactions. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use`
- Transactions are supported on replica sets (4.0+) and sharded clusters (4.2+). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use`

## Transaction Retry Pattern

- Use withTransaction() (available in all drivers) instead of manual startTransaction()/commitTransaction(). The helper automatically retries on TransientTransactionError and retries commit on UnknownTransactionCommitResult. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#transaction-retry-pattern`
- If using manual control: — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#transaction-retry-pattern`

## Transaction Anti-Patterns

- Transactions lasting >5 seconds (increases WiredTiger cache pressure and conflict risk). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#transaction-anti-patterns`
- Modifying >1,000 documents in a single transaction. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#transaction-anti-patterns`
- Using transactions for single-document operations (unnecessary overhead). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#transaction-anti-patterns`
- Not using withTransaction() helper (loses automatic retry logic). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#transaction-anti-patterns`
- Running DDL inside transactions (createCollection, createIndex). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#transaction-anti-patterns`
- Relying on transactions instead of redesigning schema for single-document atomicity. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#transaction-anti-patterns`

## Client-Level Bulk Write (MongoDB 8.0+)

- MongoDB 8.0 introduced client-level bulkWrite() that can write to multiple collections and databases in a single network round-trip: — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#client-level-bulk-write-mongodb-80`

## Bulk Operation Best Practices

- Use unordered for maximum throughput when order does not matter. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#bulk-operation-best-practices`
- Batch sizes: the driver auto-batches into 100,000-operation groups. For very large imports, chunk at the application level. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#bulk-operation-best-practices`
- Catch BulkWriteError and inspect writeErrors to identify which operations failed. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#bulk-operation-best-practices`
- Use upsert: true in UpdateOne/ReplaceOne models for idempotent loads. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#bulk-operation-best-practices`
- For multi-million-row imports, use mongoimport or mongorestore instead of driver bulk writes. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#bulk-operation-best-practices`

## Running Pipelines

- All drivers support collection.aggregate(pipeline, options). Key options: — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#running-pipelines`

## Aggregation Best Practices

- Place $match and $project as early as possible to reduce documents flowing through the pipeline. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#aggregation-best-practices`
- Use $match before $lookup to limit the join scope. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#aggregation-best-practices`
- Set allowDiskUse: true only when necessary (large groupings/sorts). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#aggregation-best-practices`
- Use maxTimeMS to prevent runaway pipelines. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#aggregation-best-practices`
- Use $merge or $out for materialized views, not in-app aggregation. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#aggregation-best-practices`
- Use explain('executionStats') to verify index utilization in $match stages. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#aggregation-best-practices`

## Change Stream Best Practices

- Always persist resume tokens - store in a separate collection or external store for crash recovery. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#change-stream-best-practices`
- Use fullDocument: 'updateLookup' when you need the complete document after an update. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#change-stream-best-practices`
- Use fullDocumentBeforeChange: 'whenAvailable' (6.0+) for audit trails. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#change-stream-best-practices`
- Filter early with $match in the pipeline to reduce network traffic. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#change-stream-best-practices`
- Handle invalidate events (dropped collection, renamed collection) by reopening the stream. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#change-stream-best-practices`
- For cross-collection CDC, watch at the database level: db.watch(). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#change-stream-best-practices`
- For cluster-wide events, watch at the client level: client.watch(). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#change-stream-best-practices`

## When to Use

- Files larger than 16 MB BSON document limit. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-1`
- Storing files alongside metadata in MongoDB without a separate file service. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-1`
- Accessing portions of large files without loading the entire file into memory. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-1`
- Keeping files synchronized across distributed deployments. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#when-to-use-1`

## How GridFS Works

- GridFS stores each file as two sets of documents: — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#how-gridfs-works`
  - fs.files - metadata (filename, length, chunkSize, uploadDate, md5, contentType) — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#how-gridfs-works`
  - fs.chunks - binary data in 255 KB chunks (default), indexed by files_id + n — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#how-gridfs-works`

## GridFS Best Practices

- Use GridFS only for files >16 MB. For smaller files, store as BinData in documents. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#gridfs-best-practices`
- Set appropriate chunkSizeBytes - smaller chunks for random-access reads, larger for sequential streaming. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#gridfs-best-practices`
- Index fs.files on fields you query (e.g., metadata.author, filename). — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#gridfs-best-practices`
- Use streaming APIs (not readAll) to avoid loading entire files into memory. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#gridfs-best-practices`
- Consider Atlas Data Lake or S3 for very large-scale file storage; GridFS is not a CDN replacement. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#gridfs-best-practices`

## mongosh Best Practices

- Use --file for repeatable scripts, not interactive copy-paste. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#mongosh-best-practices`
- Use --quiet in CI/CD to suppress the mongosh banner. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#mongosh-best-practices`
- Use printjson() for structured output; print() for plain text. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#mongosh-best-practices`
- Store maintenance scripts in version control alongside application code. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#mongosh-best-practices`
- Use --eval for one-liners in shell scripts and cron jobs. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#mongosh-best-practices`
- mongosh supports full ES2022+: use async/await, destructuring, for...of, and template literals. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#mongosh-best-practices`
- Use .mongoshrc.js for custom prompts, helpers, and default config. — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#mongosh-best-practices`

## 14. Resilient Application Checklist

- Use this checklist when reviewing any application that connects to MongoDB: — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Single client instance shared across the application — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Connection string uses SRV format for Atlas / DNS seedlist deployments — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] appName set for observability — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] retryWrites and retryReads enabled (default since 4.2) — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Write concern set to majority for durability — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Read preference matches the use case (primary for consistency, secondary for read scale) — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] maxPoolSize right-sized for deployment environment — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] serverSelectionTimeoutMS set to a reasonable value (not infinite) — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Graceful shutdown closes the client — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Error handling catches specific error types, not generic exceptions — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Duplicate key errors handled for idempotent operations — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Transactions use withTransaction() helper with automatic retry — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Projections used to limit returned fields — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Indexes cover query patterns (ESR rule) — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] maxTimeMS set on long-running queries and aggregations — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Change stream resume tokens persisted for crash recovery — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`
  - [ ] Monitoring events wired to observability stack (pool events, command events, SDAM events) — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#14-resilient-application-checklist`

## Official Driver Documentation

- Node.js Driver — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#official-driver-documentation`
- PyMongo Driver — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#official-driver-documentation`
- Java Sync Driver — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#official-driver-documentation`
- Go Driver — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#official-driver-documentation`
- C#/.NET Driver — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#official-driver-documentation`
- Rust Driver — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#official-driver-documentation`

## Connection and Pooling

- Connection Pool Overview — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#connection-and-pooling`
- Tuning Connection Pool Settings — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#connection-and-pooling`
- Node.js Connection Pools — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#connection-and-pooling`
- PyMongo Connection Pools — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#connection-and-pooling`
- Java Connection Pools — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#connection-and-pooling`

## Retryable Operations

- Retryable Writes — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-operations`
- Retryable Reads — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-operations`
- Build a Resilient Application — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#retryable-operations`

## Transactions

- Transactions - Production Considerations — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#transactions`
- Transactions - Sharded Clusters — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#transactions`
- Performance Best Practices: Transactions — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#transactions`

## GridFS

- GridFS Manual — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#gridfs`
- PyMongo GridFS — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#gridfs`

## mongosh

- Write Scripts — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#mongosh`
- Script Considerations — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#mongosh`
- Run Commands — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#mongosh`

## Specifications

- Connection Monitoring and Pooling Spec — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#specifications`
- Retryable Writes Spec — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#specifications`
- Retryable Reads Spec — source: `https://llms-explorer.com/sources/mdb-context-hub/mongodb-developer/#specifications`

## Where this helps

- Writing new application code against MongoDB in any official driver and wanting a connection, pooling, and error-handling checklist instead of reinventing resilience patterns per project. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- Debugging a customer issue involving connection exhaustion, retry behavior, or transaction failures and needing the specific error codes and retry semantics involved. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- Reviewing an existing application's MongoDB usage against a resilience checklist before a production launch or after an incident. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- Deciding whether a given write pattern needs a multi-document transaction or should instead be redesigned around single-document atomicity. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*

## Project ideas

- Build a connection-pool health dashboard by subscribing to the driver's pool monitoring events (connectionCheckedOut, connectionPoolCleared, etc.) to track saturation and wait-queue depth. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- Implement a resume-token persistence layer for a change-stream consumer so it can recover from a crash without replaying the entire stream from the beginning. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- Build a GridFS-backed file service that uses streaming APIs (not readAll) and sizes chunkSizeBytes appropriately for the access pattern (random-access vs. sequential). — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- Write a resilient-application linter that checks a codebase against the Resilient Application Checklist (single client instance, appName set, retryWrites enabled, etc.). — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*

## Antipatterns

- Opening a new MongoClient per request instead of sharing one client instance across the application — the most common pooling antipattern, since every official driver's client is already thread-safe. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- Running a transaction for longer than about 5 seconds or across more than 1,000 documents, which increases WiredTiger cache pressure and conflict risk. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- Using manual startTransaction()/commitTransaction() control instead of the withTransaction() helper, which loses the automatic retry logic for TransientTransactionError and UnknownTransactionCommitResult. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- Reaching for a multi-document transaction for what is really a single-document update, adding unnecessary overhead instead of redesigning the schema for single-document atomicity. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*

## Known issues

- updateMany and deleteMany are not retried automatically even with retryWrites enabled, because they aren't idempotent at the protocol level — application-level idempotency handling is still needed. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- DDL operations like createCollection and createIndex cannot run inside a transaction, which surprises developers who expect transactions to wrap arbitrary schema changes. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- The driver's default bulk-write auto-batching groups operations into batches of 100,000; very large imports still need application-level chunking or a dedicated tool like mongoimport instead of driver bulk writes. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
- GridFS is not a CDN replacement — for very large-scale file storage, Atlas Data Lake or S3 is the better fit, and GridFS should only be used for files that actually exceed the 16MB BSON document limit. — [source](https://llms-explorer.com/tree/mongodb-developer-patterns/) *(AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.)*
