Mongoose: 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.

Created on 11 Sep 2019  路  20Comments  路  Source: Automattic/mongoose

Hi, I just downloaded latest version of mongoose from npm and tried to run it but it gave me this warning:
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.

i added this option to my configuration but it still gives me this warning. here is my configuration

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true, useUnifiedTopology: true }).then(()=>{ console.log(connection to database established) }).catch(err=>{ console.log(db error ${err.message}); process.exit(-1) })

any clue?

Most helpful comment

This works fine for me, and no more errors.

mongoose
.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log(DB Connection Error: ${err.message});
});

All 20 comments

Same is happening to me.

There is also your thread about it on StackOverflow: https://stackoverflow.com/questions/57895175/server-discovery-and-monitoring-engine-is-deprecated/57899638#57899638

This works fine for me, and no more errors.

mongoose
.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log(DB Connection Error: ${err.message});
});

This works fine for me, and no more errors.

mongoose
.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log(DB Connection Error: ${err.message});
});

I was facing the same error and "useUnifiedTopology: true" fixed, thanks! It's already updated on the readme of the repo btw

This works fine for me, and no more errors.

mongoose
.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log(DB Connection Error: ${err.message});
});

Adding the useUnifiedTopology: true to the object after my connection URL worked just as you suggested though I was using MongoClient. Thanks!

It still error:

const options = {
    keepAlive: 1,
    useUnifiedTopology: true,
    useNewUrlParser: true,
  };
  mongoose.connect(dbUrl, options).then(() => console.log('DB connected'));

It output:

(node:14923) 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.

Anyone can help me? Thanks!

This works fine for me, and no more errors.
mongoose
.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log(DB Connection Error: ${err.message});
});

I was facing the same error and "useUnifiedTopology: true" fixed, thanks! It's already updated on the readme of the repo btw

@jiobiagba Yes i have tried your solution now my node.js server is not showing me any error simply it is showing 'The Server Is Running On Port:7070'.

Thanks for the solution hope no error will come.

I was having the same problem and when inserting useUnifiedTopology: true, stopped showing terminal error.

Folow code:

mongo() {
this.mongoConnection = mongoose.connect(process.env.MONGO_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: true,
});
}

This parameter does not work with our replica set URL:

error:getaddrinfo ENOTFOUND .xyz.net .xyz.net:12345

Are there any replica set changes needed on top of this parameter?

@cbratschi can you please explain in details cause i want exact error and code so further i can try to help you.

I too faced the same issue and after inserting useUnifiedTopology: true, I don't face any errors, but the console log in the .then() block does not get executed.

mongoose.connect(MONGODB_URI,{ useNewUrlParser: true, useUnifiedTopology: true })
.then(()=> console.log('Database Connection Successful!!'))
.catch(err => console.error(err));

@guhandelta none of code is executing (.then() and .catch()) ?

Yes... none, I don't get any console logs on my cmd prompt or errors.

server/index.js =>
node indexjs

npm run dev =>
npm run dev

@cbratschi can you please explain in details cause i want exact error and code so further i can try to help you.

I only see these lines in the log message and the stacktrace contains the same information:

    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
(node:7740) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

This is the URL we are using (anonymized):

mongodb://user:[email protected]:27018,.xyz.net:27019,.xyz.net:27020/account?replicaSet=mongors&readPreference=primaryPreferred

Update: it works if the abc sub-domain part is repeated in the URI. For instance:

mongodb://user:[email protected]:27018,abc.xyz.net:27019,abc.xyz.net:27020/account?replicaSet=mongors&readPreference=primaryPreferred

@cbratschi can you please review your code once cause it will direct give the idea here the error is saying the same.it might possible that you are leaving some code for handling catch or async code or you might have missed promise which is compulsory in non-blocking.

Hello, I have:

.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log(Error, err.message);
});

When it starts I get at the console:
[Function: Error] { stackTraceLimit: 16, prepareStackTrace: undefined } connection 0 to acccluster-shard-00-01-xx47j.mongodb.net:27017 closed

@cbratschi can you please review your code once cause it will direct give the idea here the error is saying the same.it might possible that you are leaving some code for handling catch or async code or you might have missed promise which is compulsory in non-blocking.

The subdomain was not parsed correctly in the implementation. Switching to full domains it works fine now (see my last comment). So the implementation is not fully standard compliant.

This works fine for me, and no more errors.

mongoose
.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log(DB Connection Error: ${err.message});
});

I was using the method indicated in this link, but it didn't work. After using the useUnifiedTopology option in the mongoose.connect() it worked!

I found my way here because I am having the same problem - I just switched to the latest version of mongodb, and I get the warning. I added useUnifiedTopology to the options passed to the MongoClient constructor, but the warning persists. The connection to the remote database seems fine, the operations are succeeding and there are no other error messages or warnings. My url is: mongodb://myUsername:[email protected]:27017/myDatabaseName

I fixed this bug with this:

mongoose.connect('mongodb+srv: {...}', {
useNewUrlParser: true,
useUnifiedTopology: true
});

Please follow the instructions on the docs here: https://mongoosejs.com/docs/deprecations.html

Was this page helpful?
0 / 5 - 0 ratings