Server Discovery And Monitoring engine is deprecated

后端 未结 23 1060
执笔经年
执笔经年 2020-12-01 00:54

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

mongoose.connect(process.env.MONGO_URI, {
   useNewUrlParser: true,
   useUnifiedTopol         


        
相关标签:
23条回答
  • 2020-12-01 01:02

    It is important to run your mongod command and keep the server running. If not, you will still be seeing this error.

    I attach you my code:

    const mongodb = require('mongodb')
    const MongoClient = mongodb.MongoClient
    
    const connectionURL = 'mongodb://127.0.0.1:27017'
    const databaseName = 'task-manager'
    
    MongoClient.connect(connectionURL, {useNewUrlParser: true, useUnifiedTopology: true}, (error, client) => {
        if(error) {
            return console.log('Error connecting to the server.')
        }
    
        console.log('Succesfully connected.')
    })

    0 讨论(0)
  • 2020-12-01 01:04

    If you are using a MongoDB server then after using connect in the cluster clock on connect and finding the URL, the URL will be somehing like this

    <mongodb+srv://Rohan:<password>@cluster0-3kcv6.mongodb.net/<dbname>?retryWrites=true&w=majority>
    

    In this case, don't forget to replace the password with your database password and db name and then use

    const client = new MongoClient(url,{useUnifiedTopology:true});
    
    0 讨论(0)
  • 2020-12-01 01:06
    const url = 'mongodb://localhost:27017';
    const client = new MongoClient(url);
    

    Cut the upper 2nd line then Just Replace that's line

    const client = new MongoClient(url, { useUnifiedTopology: true });
    
    0 讨论(0)
  • 2020-12-01 01:07

    This will work:

    // Connect to Mongo
    mongoose.set("useNewUrlParser", true);
    mongoose.set("useUnifiedTopology", true);
    
    mongoose
      .connect(db) // Connection String here
      .then(() => console.log("MongoDB Connected..."))
      .catch(() => console.log(err));
    
    0 讨论(0)
  • 2020-12-01 01:08

    It is simple , remove the code that you have used and use the below code :

    const url = 'mongodb://localhost:27017';
    var dbConn = mongodb.MongoClient.connect(url, {useUnifiedTopology: true});
    
    0 讨论(0)
  • 2020-12-01 01:09

    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.

    0 讨论(0)
提交回复
热议问题