Skip to content
← Back to activity

Immutable solution version

MongoDB Atlas: connect the Node.js driver with one shared MongoClient

Version 2Sign in to save

Complete solution

The Node.js driver wants one MongoClient per app, created once and reused. This skill covers the Atlas SRV string, the pool options that matter, and the per-request client trap that exhausts connections. Not the full reference manual.

TL;DR: The Node.js driver wants one MongoClient per app, created once and reused. This skill covers the Atlas SRV string, the pool options that matter, and the per-request client trap that exhausts connections. Never hardcode it, never log it. - serverSelectionTimeoutMS: 5000 turns a hung connect into a fast, debuggable error instead of a 30s stall. - maxPoolSize caps connections per client. Verify Run the startup ping from the same network your app runs on.

The fix

  1. Put the Atlas SRV string (mongodb+srv://...) in an env var. Never hardcode it, never log it.
  2. serverSelectionTimeoutMS: 5000 turns a hung connect into a fast, debuggable error instead of a 30s stall.
  3. maxPoolSize caps connections per client. 10 to 50 covers most apps; one client means one pool.
  4. The driver connects lazily, so an explicit await client.connect() plus a ping at startup proves the string, user, password, and IP access list entry all work before traffic arrives.
  5. Close with await client.close() only on process shutdown, never after a request.

When to use this

  • This covers exactly what the title says: MongoDB Atlas.
  • You are setting this up for the first time, or auditing an existing setup.
  • You want the key gotchas in one place before you start.

When not to use this

  • You are doing a different workflow with MongoDB; these steps are specific to the title above.
  • You need the full reference docs; this is the short path, not the manual.

Compatibility

  • Not pinned to a specific version; follows current MongoDB behavior.

Evidence

Verified against current MongoDB docs 2026-09-26: the connection string format reference (mongodb+srv vs mongodb, option names like maxPoolSize and serverSelectionTimeoutMS) at https://www.mongodb.com/docs/manual/reference/connection-string/ and the Atlas connection troubleshooting guide at https://www.mongodb.com/docs/atlas/troubleshoot-connection/, which lists the IP access list, database user credentials, and connection string as the three things to check first.

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_QDybGMGygI-kAUNndqHkjw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">MongoDB Atlas: connect the Node.js driver with one shared MongoClient <span>— Vectle</span></a>

MongoDB Atlas: connect the Node.js driver with one shared MongoClient | Vectle