0
votes

So when running Nodemon after setting up an ExpressJS & MongoDB client, I keep getting the

"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'm not really sure where to insert this code into my server.js file.

2

2 Answers

1
votes

You'll want to insert it where you're creating your mongo connection.

For example (using mongoose)...

const mongoose = require("mongoose");

const { DATABASE } = process.env; // this a mongodb connection string that varies upon the NODE environment

const options = {
  useNewUrlParser: true, // avoids DeprecationWarning: current URL string parser is deprecated
  useCreateIndex: true, // avoids DeprecationWarning: collection.ensureIndex is deprecated.
  useFindAndModify: false, // avoids DeprecationWarning: collection.findAndModify is deprecated.
  useUnifiedTopology: true // avoids DeprecationWarning: current Server Discovery and Monitoring engine is deprecated
};

mongoose.connect(DATABASE, options); // connect to our mongodb database

Here's a Fullstack MERN boilerplate I created and use for my projects, which has an example of how to connect to a local mongo database. You can use it as a reference, if needed.

1
votes

If you're using mongodb not mongoose, then only need to pass { useUnifiedTopology: true } in your MongoClient.connection options:

MongoClient.connect('mongodb://localhost:27017', { useUnifiedTopology: true })
  .then(client => {
    // do some stuff
  }).catch(error => {
    // do some stuff
  })

I hope it can help you.