Question 47 / 50
ImportantMediumScenarioNode.js
How should a Node.js web app share MongoDB connections across requests, and what goes in environment variables?
Think about it first.
Short answer
One MongoClient per process, reused for every request. The URI, database name, and credentials live in environment variables — never in source control. The pool size is a client option, not a new client per route.
Why?
Under load the pool multiplexes sockets. Too-large pools exhaust mongod connections when you run many Node instances. Tune maxPoolSize against replica-set connection limits. In tests, use a separate URI so you never point a test runner at production.
Interview tip
Mention pool reuse and secrets in the same answer. Interviewers like seeing both operations and security.
Common mistake
Hard-coding mongodb://localhost in a module that gets deployed.
How did you do?