Maaz Zindani

Loading the index…

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

Mongoose query hangs and then fails with buffering timed out

ErrorOperation `products.find()` buffering timed out after 10000ms

Short answer

Mongoose queues every query until it connects to MongoDB, and gives up after bufferTimeoutMS, 10000 ms by default. So the error means the connection never became ready. Check that connect() is called, awaited and not failing, that the host is reachable, and that the address is 127.0.0.1 for a local server.

  • Applies to Mongoose 8, MongoDB 7, Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Connect in a try/catch and print the result before any query runs. The real connection error shows up here, instead of ten seconds later.

    try {
      await mongoose.connect(process.env.MONGODB_URI);
      console.log('connected');
    } catch (err) {
      console.error('connect failed', err);
    }
  2. 2Match what you see, then jump to its fix

    1. 1No 'connected' line is printed, and the queries start immediatelyconnect() is never called, or runs after the queriesFix
    2. 2A rejected promise or an error event on the connection, then the buffering timeout laterThe first connection failed and nothing handled the errorFix
    3. 3Local server, mongod is running, the URI says localhost, and the connection never completeslocalhost resolves to the IPv6 address ::1Fix
    4. 4You want a query on a closed connection to fail immediately, not waitBuffering hides a mistake you would rather see at onceFix

What does the error look like?

  • Operation `products.find()` buffering timed out after 10000ms

Cause 1 of 4

connect() is never called, or runs after the queries

1Check

Mongoose lets you use a model before connecting and does not throw by default. The queries wait in a queue that only drains after a successful connection.

grep -rn "mongoose.connect" src/ index.js

2Fix

Call connect once at startup and start serving requests only after it resolves.

import mongoose from 'mongoose';

await mongoose.connect(process.env.MONGODB_URI);
app.listen(3000);

3Verify

The first query returns at once instead of after ten seconds.

curl -i http://127.0.0.1:3000/products

Confirmed by mongoosejs.com

Did this fix it?

Cause 2 of 4

The first connection failed and nothing handled the error

1Check

If the first connection fails, Mongoose emits an error and the connect promise rejects. It does not retry on its own, so the model stays buffered.

mongosh "$MONGODB_URI" --eval "db.runCommand({ ping: 1 })"

2Fix

Handle the rejection, fix the reason it printed, and shorten how long the driver searches for a server so the failure is quick.

try {
  await mongoose.connect(process.env.MONGODB_URI, {
    serverSelectionTimeoutMS: 5000,
  });
} catch (err) {
  console.error(err);
  process.exit(1);
}

3Verify

A wrong address now fails after about five seconds with the real error.

node server.mjs

Confirmed by mongoosejs.com

Did this fix it?

Cause 3 of 4

localhost resolves to the IPv6 address ::1

1Check

The Mongoose docs recommend 127.0.0.1 instead of localhost for a local server, because Node.js 18 and later may resolve localhost to the IPv6 address ::1.

grep -rn "localhost:27017" .env src/

2Fix

Use 127.0.0.1 in the connection string.

MONGODB_URI=mongodb://127.0.0.1:27017/myapp

3Verify

Start the app again and watch for the connected line.

node server.mjs

Confirmed by mongoosejs.com

Did this fix it?

Cause 4 of 4

Buffering hides a mistake you would rather see at once

1Check

All collection actions are queued until Mongoose connects. The FAQ says the root of this error is not connecting to MongoDB.

grep -rn "bufferCommands" src/

2Fix

Turn buffering off, so a query without a connection fails at once with a clear error. Do this only after connect() is awaited at startup, or all early queries will fail.

mongoose.set('bufferCommands', false);
await mongoose.connect(process.env.MONGODB_URI);

3Verify

A query made before connect now fails straight away.

node server.mjs

Confirmed by mongoosejs.com

Did this fix it?

How do I stop it coming back?

  • Await connect() before the server accepts traffic, and fail the start-up if it rejects.
  • Use 127.0.0.1 for local MongoDB in connection strings.
  • Keep the connection string in one environment variable and log the host, never the password.
  • Listen to mongoose.connection error events so later drops show up in your logs.

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.