Mongoose query hangs and then fails with buffering timed out
Operation `products.find()` buffering timed out after 10000msShort 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.
Fix it fast
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); }2Match what you see, then jump to its fix
- 1
No 'connected' line is printed, and the queries start immediatelyconnect() is never called, or runs after the queriesFix - 2
A rejected promise or an error event on the connection, then the buffering timeout laterThe first connection failed and nothing handled the errorFix - 3
Local server, mongod is running, the URI says localhost, and the connection never completeslocalhost resolves to the IPv6 address ::1Fix - 4
You want a query on a closed connection to fail immediately, not waitBuffering hides a mistake you would rather see at onceFix
- 1
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.js2Fix
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/productsConfirmed by mongoosejs.com
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.mjsConfirmed by mongoosejs.com
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/myapp3Verify
Start the app again and watch for the connected line.
node server.mjsConfirmed by mongoosejs.com
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.mjsConfirmed by mongoosejs.com
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 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