Skip to content

Advanced Topics Hub

This page links to detailed pages for each advanced MongoDB topic.

flowchart TB
subgraph Advanced[Advanced MongoDB Topics]
Tx[Transactions<br/>Multi-doc ACID]
RS[Replica Sets<br/>High Availability]
Shard[Sharding<br/>Horizontal Scaling]
CS[Change Streams<br/>Real-time Changes]
RW[Read/Write Concern<br/>Durability & Consistency]
Perf[Performance & explain<br/>Query Optimization]
end
Advanced --> Practice[Production Readiness ✅]
style Tx fill:#7c3aed,color:#fff
style RS fill:#3b82f6,color:#fff
style Shard fill:#059669,color:#fff
style CS fill:#f59e0b,color:#fff
style RW fill:#ec4899,color:#fff
style Perf fill:#06b6d4,color:#fff
style Practice fill:#10b981,color:#fff
TopicWhat it covers
Replication & Replica SetsPrimary/secondary/arbiter, automatic failover, high availability
ShardingSplitting data across servers, shard key, chunks, mongos router
TransactionsMulti-document ACID transactions, sessions, commit/abort
Read & Write ConcernWrite concern (w, j), read concern, read preference — durability knobs
Change StreamsReact to data changes in real time, resume tokens
Performance & explain()explain(), covered queries, spotting slow queries
CodeErrorCommon Cause
11000Duplicate keyInserting a document that violates a unique index
121Document validation failureDocument doesn’t match $jsonSchema validation
16500Too many requestsAtlas free tier throttling
2BadValueInvalid field/operator in query
13UnauthorizedAuthentication/permission issue
// Handle duplicate key error
try {
await User.create({ email: "existing@example.com" });
} catch (err) {
if (err.code === 11000) {
const field = Object.keys(err.keyValue)[0];
throw new Error(`${field} already exists`);
}
throw err;
}
1. SCHEMA DESIGN
✅ Design schemas around your query patterns (query-first design)
✅ Embed data that's always accessed together
✅ Reference data that grows unboundedly
2. INDEXING
✅ Index every field used in find(), sort(), and $lookup
✅ Use compound indexes that match your queries (ESR rule)
✅ Use explain() to verify index usage
3. QUERIES
✅ Use projection — only return fields you need
✅ Use pagination (limit + skip or cursor-based)
✅ Put $match early in aggregation pipelines
❌ Avoid $where (JavaScript execution — very slow)
4. CONNECTIONS
✅ Use connection pooling (Mongoose does this by default)
✅ Set appropriate pool size for your app's concurrency
5. OPERATIONS
✅ Use bulk operations instead of loops
✅ Use $inc, $push instead of read-modify-write
✅ Use TTL indexes for auto-expiring data

  • MongoDB’s advanced features are now in dedicated pages — use the links above
  • Replica sets give you high availability; sharding gives you horizontal scale
  • Transactions ensure multi-document atomicity; change streams give real-time events
  • Always use explain() to debug query performance
  • The most common errors are duplicate keys (11000) and validation failures (121)

📖 Full index: MongoDB Overview →