MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Debugging the mongoose Connection to atlas

When I connect and it tells me very little about failure or success. As it fails a lot connecting from my windows 10. (same code) It has nothing to do with whitelisting because the IP isn’t changing and is already whitelisted. What can I do to understand where the driver is spending time during connecting? Is this indicative of the free Atlas platform.

On success, I generally get this: ( I added timings for benefit)

Connecting to Mongoose: 10:26:01 AM
App running on port 3000… 22 ms 10:26:01 AM
DB connection successful! 326 ms 10:26:02 AM

Connecting to Mongoose: 11:32:31 AM
App running on port 3000… 11:32:31 AM: 18 ms : pass
DB connection successful! 11:33:06 AM: 35111 ms : fail

Connecting to Mongoose: 11:36:08 AM
App running on port 3000… 11:36:08 AM: 20 ms : pass
UNHANDLED REJECTION! 💥 Shutting down… 11:37:23 AM: 75120 ms : fail
Error queryTxt ETIMEOUT cluster0-vplwu.mongodb.net Error: queryTxt ETIMEOUT cluster0-vplwu.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:202:19)

My code below Mongoose 5.9.14 to Atlas

const DB = process.env.DATABASE.replace(
‘’,
process.env.DATABASE_PASSWORD
);

DB=mongodb+srv://username:@cluster0-vplwu.mongodb.net/natours?retryWrites=true

mongoose
.connect(DB, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
serverSelectionTimeoutMS: 5000,
family: 4
})
.then(() => console.log(‘DB connection successful!’, tm(started)));

const port = process.env.PORT || 3000;

const server = app.listen(port, () => {
console.log(App running on port ${port}...);
});

I’m seeing failure rates on connections over 20% and that is being kind. On rare occasions, I see sub-second response times. Generally connections are made in 15 or 35 seconds. This is pretty consistent. Any 35-second connection won’t run on Heroku. I’m not sure a 15-second connection will get in done on Heroku.