
Schema design, indexing, and query patterns for MongoDB with Node.js drivers.
Even though MongoDB is schemaless, defining a clear schema (e.g. with Mongoose) improves consistency and allows validation. Prefer embedding for one-to-few and frequently accessed data; use references for one-to-many or when documents grow large.
Create indexes for queries you run often and for sort fields. Compound indexes should follow the equality-sort-range rule. Avoid over-indexing—each index adds write cost. Use explain() to verify query plans and index usage.
Use a single MongoClient instance per process and reuse it across requests. Configure pool size based on your app's concurrency and server capacity. In serverless, be aware of connection limits and consider a proxy or connection pooling service if needed.
Use aggregation for complex analytics, joins (lookup), and reshaping data. Break pipelines into stages and add indexes that support $match and $sort early in the pipeline. Use $project to limit fields and reduce data movement.
When you need multi-document ACID guarantees, use transactions. Keep transaction duration short and avoid long-running operations inside them. In replica sets, transactions are fully supported; in sharded clusters, plan for cross-shard transactions carefully.