node.jsmongodbwarnings

How to resolve this mongodb Warning issue in node js and How to traceback about errors in nodeJs?


    const connectDB = ()=>{
        mongoose
        .connect(`mongodb://${config.db.host}:${config.db.port}/${config.db.name}`,
        {useUnifiedTopology: true, useNewUrlParser: true})
        .then(()=>{
            console.log("DB connection successful.");
        })
        .catch((err)=>{
            console.log(`DB connection error:${err}`);
        });
    }

This is my mongodb connectivity connectivity code using mongoose library and I am getting the following warnings:

(node:17692) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version (Use **node --trace-warnings ...** to show where the warning was created) (node:17692) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version

Please explain me what is this warning about and How to get rid of this? And also explain me how to use "node --trace-warnings" commands in nodeJs?

NOTE: This db connectivity code runs successful and I can perform operations on them but I want to just get rid of this warning.

I have browse about this on Web but can't get relevant information regarding this.

NOTE:- This db connectivity code runs successful and I can perform operations on them but I want to just get rid of this warning.


Solution

  • The following options were removed and were only relevant in version 5.x so if you are using a more modern version of mongoose such as 7.x or even 8.x then you don't need to include any of these:

    {
       useNewUrlParser: true, 
       useUnifiedTopology: true,
       useFindAndModify: false,
       useCreateIndex: true,  
    }
    

    useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex are no longer supported options. Mongoose 6 always behaves as if useNewUrlParser, useUnifiedTopology, and useCreateIndex are true, and useFindAndModify is false. Please remove these options from your code.

    Simply remove them from your connection like so:

    const connectDB = ()=>{
       mongoose.connect(`mongodb://${config.db.host}:${config.db.port}/${config.db.name}`)
       .then(()=>{
          console.log("DB connection successful.");
       })
       .catch((err)=>{
          console.log(`DB connection error:${err}`);
       });
    }