MongoNotConnectedError: the MongoClient is used before it has connected
MongoNotConnectedError: MongoClient must be connected to perform this operationShort answer
The driver throws MongoNotConnectedError when an operation runs on a client that has not yet connected. Before driver v4.7 you had to call connect() yourself; since then CRUD calls connect for you. Upgrade, or await client.connect() before the first operation, and never fire queries while the promise is pending.
Fix it fast
1Find out which cause you have
Check the installed driver version, then look at how the client is connected. The version tells you whether connect() is optional.
npm ls mongodb grep -rn "connect()" src2Match what you see, then jump to its fix
What does the error look like?
MongoNotConnectedError: MongoClient must be connected to perform this operationWorks in one script and fails in another that skips the connect step
Cause 1 of 2
An old driver that does not connect for you
1Check
The driver docs state that connect() is optional starting in v4.7, because the driver calls it when you run CRUD operations. On older versions, no one does.
npm ls mongodb2Fix
Upgrade the driver, or call connect() explicitly as the code below does.
npm install mongodb@latest3Verify
Run an operation, for example a ping, on a fresh client.
node -e "const {MongoClient}=require('mongodb');(async()=>{const c=new MongoClient(process.env.MONGO_URI);console.log(await c.db('admin').command({ping:1}));await c.close();})()"Confirmed by mongodb.com
Cause 2 of 2
connect() is not awaited before the first operation
1Check
client.connect() returns a promise. If a query starts while it is pending, or in module code that never waits for it, the client can still be unconnected when the operation runs.
grep -rn -B2 -A4 "client.connect\|MongoClient(" src2Fix
Await the connection once, at startup, and reuse that one client everywhere instead of creating a client per request.
const client = new MongoClient(process.env.MONGO_URI);
await client.connect();
const users = client.db('app').collection('users');
await users.findOne({});3Verify
Start the app and run the first query immediately.
node src/index.jsConfirmed by mongodb.com
How do I stop it coming back?
- Create one MongoClient per process; the driver documents each client as a connection pool meant to be reused.
- Await the connection in your start-up code, and only then begin listening for requests or running tests.
- Close the client in a finally block, as the driver docs show, only after pending operations have finished.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix