Server Discovery And Monitoring engine is deprecat

2020-02-08 05:04发布

I am using Mongoose with my Node.js app and this is my configuration:

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

but in the console it still gives me the 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.

What is the problem? I was not using useUnifiedTopology before but now it shows up in the console. I added it to the config but it still gives me this warning, why? I do not even use MongoClient.

Edit

As Felipe Plets answered there was a problem in Mongoose and they fixed this bug in later versions. So you can solve problem by updating mongoose version.

11条回答
我只想做你的唯一
2楼-- · 2020-02-08 05:25

You Can Try async await

const connectDB = async () => {
    try {
        await mongoose.connect(<database url>, {
            useNewUrlParser: true,
            useCreateIndex: true,
            useUnifiedTopology: true,
            useFindAndModify: false
        });
        console.log("MongoDB Conected")
    } catch (err) {
        console.error(err.message);
        process.exit(1);
    }
};

查看更多
男人必须洒脱
3楼-- · 2020-02-08 05:27

This solved my problem.

 const url = 'mongodb://localhost:27017';

 const client = new MongoClient(url, {useUnifiedTopology: true});
查看更多
beautiful°
4楼-- · 2020-02-08 05:27
mongoose.connect('mongodb://localhost:27017/Tododb', { useNewUrlParser: true, useUnifiedTopology: true });

Will remove following errors:-

(node:7481) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

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

查看更多
家丑人穷心不美
5楼-- · 2020-02-08 05:27

Setting mongoose connect useUnifiedTopology: true option

  import mongoose from 'mongoose';

        const server = '127.0.0.1:27017'; // REPLACE WITH YOUR DB SERVER
        const database = 'DBName'; // REPLACE WITH YOUR DB NAME
        class Database {
          constructor() {
            this._connect();
          }
          _connect() {
            mongoose.Promise = global.Promise;
            // * Local DB SERVER *
            mongoose
              .connect(`mongodb://${server}/${database}`, {
                useNewUrlParser: true,
                useCreateIndex: true,
                useUnifiedTopology: true
              })
              .then(
                () => console.log(`mongoose version: ${mongoose.version}`),
                console.log('Database connection successful'),
              )
              .catch(err => console.error('Database connection error', err));   
          }
        }
        module.exports = new Database();
查看更多
Emotional °昔
6楼-- · 2020-02-08 05:28
   const mongo = require('mongodb').MongoClient;

   mongo.connect(process.env.DATABASE,{useUnifiedTopology: true, 
   useNewUrlParser: true}, (err, db) => {
      if(err) {
    console.log('Database error: ' + err);
   } else {
    console.log('Successful database connection');
      auth(app, db)
      routes(app, db)

   app.listen(process.env.PORT || 3000, () => {
      console.log("Listening on port " + process.env.PORT);
    });  

}});

查看更多
我只想做你的唯一
7楼-- · 2020-02-08 05:28

Update

Mongoose 5.7.1 was release and seems to fix the issue, so setting up the useUnifiedTopology option work as expected.

mongoose.connect(mongoConnectionString, {useNewUrlParser: true, useUnifiedTopology: true});

Original answer

I was facing the same issue and decided to deep dive on Mongoose code: https://github.com/Automattic/mongoose/search?q=useUnifiedTopology&unscoped_q=useUnifiedTopology

Seems to be an option added on version 5.7 of Mongoose and not well documented yet. I could not even find it mentioned in the library history https://github.com/Automattic/mongoose/blob/master/History.md

According to a comment in the code:

  • @param {Boolean} [options.useUnifiedTopology=false] False by default. Set to true to opt in to the MongoDB driver's replica set and sharded cluster monitoring engine.

There is also an issue on the project GitHub about this error: https://github.com/Automattic/mongoose/issues/8156

In my case I don't use Mongoose in a replica set or sharded cluster and though the option should be false. But if false it complains the setting should be true. Once is true it still don't work, probably because my database does not run on a replica set or sharded cluster.

I've downgraded to 5.6.13 and my project is back working fine. So the only option I see for now is to downgrade it and wait for the fix to update for a newer version.

查看更多
登录 后发表回答