Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

MongoNotConnectedError: the MongoClient is used before it has connected

ErrorMongoNotConnectedError: MongoClient must be connected to perform this operation

Short 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.

  • Applies to mongodb driver 4.x to 6.x, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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()" src
  2. 2Match what you see, then jump to its fix

    1. 1npm ls shows mongodb below 4.7 and the code never calls connect()An old driver that does not connect for youFix
    2. 2The error appears on startup or in the first test, then goes away on retryconnect() is not awaited before the first operationFix

What does the error look like?

  • MongoNotConnectedError: MongoClient must be connected to perform this operation
  • Works 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 mongodb

2Fix

Upgrade the driver, or call connect() explicitly as the code below does.

npm install mongodb@latest

3Verify

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

Did this fix it?

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(" src

2Fix

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.js

Confirmed by mongodb.com

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.