question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

{ useUnifiedTopology: true } leads to MongoDB connection error: MongoTimeoutError: Server selection timed out after 30000 ms

See original GitHub issue

Do you want to request a feature or report a bug?

A bug.

What is the current behavior?

After updating to Mongoose 5.7.1, the following warning appeared:

(node:41563) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
    at parseFn (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/operations/connect.js:312:5)
    at parseConnectionString (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/core/uri_parser.js:628:3)
    at connect (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/operations/connect.js:266:3)
    at ConnectOperation.execute (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/operations/connect.js:191:5)
    at executeOperation (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/operations/execute_operation.js:83:26)
    at MongoClient.connect (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:216:10)
    at Promise (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/lib/connection.js:632:12)
    at Promise._execute (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/bluebird/js/release/debuggability.js:313:9)
    at Promise._resolveFromExecutor (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/bluebird/js/release/promise.js:488:18)
    at new Promise (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/bluebird/js/release/promise.js:79:10)
    at NativeConnection.Connection.openUri (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/lib/connection.js:629:19)
    at Mongoose.connect (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/lib/index.js:327:15)
    at Object.connect (/Users/tschaffter/dev/PHCCollaborationPortal/server/app.js:17:44)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Module._compile (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.newLoader [as .js] (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/Users/tschaffter/dev/PHCCollaborationPortal/server/index.js:12:28)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

I added useUnifiedTopology: true to my mongoose config object as suggested by the message, even though I’m not using MongoDB replica set or sharded cluster. Here is how I configure and connect using mongoose:

        options: {
            // https://mongoosejs.com/docs/deprecations.html
            useNewUrlParser: true,
            useFindAndModify: false,
            useCreateIndex: true,
            useUnifiedTopology: true,
            reconnectTries: 30,
            reconnectInterval: 500, // in ms
        }
    },

and here is where I used this mongo.options object:

// Connect to MongoDB
const mongooseConnectionPromise = mongoose.connect(config.mongo.uri, config.mongo.options);
mongoose.connection.on('error', err => {
    console.error(`MongoDB connection error: ${err}`);
    process.exit(-1); // eslint-disable-line no-process-exit
});

The warning is gone but the issue is that mongoose is now no longer able to connect:

MongoDB connection error: MongoTimeoutError: Server selection timed out after 30000 ms
[nodemon] app crashed - waiting for file changes before starting...

Here is how I run MongoDB using docker during development:

docker run -p ${MONGO_PORT}:${MONGO_PORT} --name mongo -d mongo

If the current behavior is a bug, please provide the steps to reproduce.

See above

What is the expected behavior?

Mongoose should remove the deprecation warning and run fine when using useUnifiedTopology: true as mongoose suggests.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version.

Node: v10.16.2 Mongoose: 5.7.1 (latest) MongoDB: db version v4.2.0

Related issues:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:17
  • Comments:78

github_iconTop GitHub Comments

17reactions
mbroadstcommented, Nov 6, 2019

Hi! My name is Matt and I’m on the drivers team at MongoDB. We’re very sorry for the outages y’all have been facing, and I wanted to give a little update on the issue:

We’re in the process of trying to phase out our legacy topology types which has turned out to be a very surgical process. Phasing out these types will help greatly reduce the maintenance burden of the driver, and our hope is that means a more stable and efficient driver for our users. In particular, while introducing the “Unified Topology” we did not also introduce the rewrite of the connection pool in an effort to reduce the potential for error. It turns out the connection pool was more tightly coupled to the topology types than we anticipated and as a result we experienced some regressions, particularly around server monitoring.

This morning I pushed a v3.3.4-rc0 of the driver which should address the issues you have been facing. We would be extremely grateful if you could try this version out and report back with your experience. I have left comments on each of the existing tickets related to this issue on NODE-2267, but please feel free to open additional issues there if you experience them.

12reactions
crobinson42commented, Oct 30, 2019

This is an issue with replica set MongoDB servers and a solution is being worked on here by MongoDB engineers

https://jira.mongodb.org/browse/NODE-2267

@vkarpov15 can probably close this issue because it has nothing to do with Mongoose.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoDB connection error: MongoTimeoutError: Server ...
Mostly "Server selection timed out after 30000 ms at Timeout._onTimeout" comes whenever your above things are not matched with your server ...
Read more >
Server selection timed out after 30000 ms - MongoDB Atlas
We are seeing intermediate issue of pod crashing with error stating MongoTimeoutError: Server selection timed out after 30000 ms when it is ...
Read more >
Server selection timed out after 30000 ms · Issue #8381 ...
After updating to Mongoose 5.7.12, the following warning appeared: MongoDB error when connecting: MongoTimeoutError: Server selection timed ...
Read more >
Server selection timed out after 30000 ms - MongoDB Jira
Hello,. I'm getting this error message from mogodb module. MongoTimeoutError: Server selection timed out after 30000 ms. at Timeout.
Read more >
server selection timed out after 30000 ms mongodb - You.com
I have a mongoDB atlas for application. I have connected to atlas from local env by whitelisting my IP on MongoDB atlas also,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found